Spark 时间窗口的 worldcount 在 windows 环境下运行

环境

监听端口(参考博客时,直接翻到最下边):

C:\Users\Feng>nc -l -p 12345
hello world hello
world world world
hell
helll
lll
ll ll ll
ll ll oo

结果展示:

-------------------------------------------
Time: 1581494862000 ms
-------------------------------------------
(ll,5)
(lll,1)
(oo,1)
(helll,1)
(hell,1)

pom 依赖

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spark.version>2.4.4</spark.version>
  </properties>

    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming_2.11</artifactId>
      <version>${spark.version}</version>
    </dependency>

Scala 代码

package org.feng.stream.window
import org.apache.spark._
import org.apache.spark.streaming._

/**
  * Created by Feng on 2019/12/3 15:26
  * CurrentProject's name is spark
  * 时间窗口:3秒一个批次,窗口12秒,滑步6秒
  */
object WordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("WordCount")
    val streamingContext = new StreamingContext(sparkConf, Seconds(3))
    // 设置检查点
    streamingContext.checkpoint(".")

    val lines = streamingContext.socketTextStream("localhost", 12345)
    val words = lines.flatMap(_.split(" ")).map(x => (x, 1))
    val result = words.reduceByKeyAndWindow((x:Int, y:Int) => x + y, Seconds(12), Seconds(6))

    result.print()
    streamingContext.start()
    streamingContext.awaitTermination()
  }
}


附加:计算总的wordcount

package org.feng.stream
import org.apache.spark._
import org.apache.spark.streaming._
/**
  * Created by Feng on 2019/12/3 16:04
  * CurrentProject's name is spark
  */
object StateWordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("StateWordCount")

    val streamingContext = new StreamingContext(sparkConf, Seconds(3))
    streamingContext.checkpoint(".")
    val lines = streamingContext.socketTextStream("localhost", 12345)


    val fun = (values: Seq[Int], state: Option[Int]) => {
      val currentCount = values.sum
      val previousCount = state.getOrElse(0)
      Some(currentCount + previousCount)
    }

    lines.flatMap(_.split(" ")).map(word => (word, 1)).updateStateByKey(fun).print()

    streamingContext.start()
    streamingContext.awaitTermination()
  }
}


发布了108 篇原创文章 · 获赞 117 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/FBB360JAVA/article/details/104280470