Spark Streaming combat reduceByKeyAndWindow

reduceByKeyAndWindow: window function

  1.  Graphic

The calculation process of the sliding window conversion operation is shown in the figure below,

We can set the length of a sliding window (that is, the duration of the window) in advance, and set the interval of the sliding window (how often to perform a calculation),

For example, set the length of the sliding window (that is, the duration of the window) to 24H, and set the interval of the sliding window (how often to perform calculations) to 1H

So the meaning is: calculate the latest 24H data every 1H

 

Code:

package SparkStrimng

import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by 一个蔡狗 on 2020/4/10.
  */
object SparkStreaming_03 {
  def main(args: Array[String]): Unit = {

    //1.创建StreamingContext
    //spark.master should be set as local[n], n > 1
    val conf = new SparkConf().setAppName("wc").setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")
    val ssc = new StreamingContext(sc,Seconds(5))//5表示5秒中对数据进行切分形成一个RDD
    //2.监听Socket接收数据
    //ReceiverInputDStream就是接收到的所有的数据组成的RDD,封装成了DStream,接下来对DStream进行操作就是对RDD进行操作
    val dataDStream: ReceiverInputDStream[String] = ssc.socketTextStream("node001",9999)
    //3.操作数据
    val wordDStream: DStream[String] = dataDStream.flatMap(_.split(" "))
    val wordAndOneDStream: DStream[(String, Int)] = wordDStream.map((_,1))

    val wordAndCount: DStream[(String, Int)] = wordAndOneDStream.reduceByKeyAndWindow((a:Int,b:Int)=>a+b,Seconds(10),Seconds(5))
    //4.使用窗口函数进行WordCount计数
    //reduceFunc: (V, V) => V,集合函数
    //windowDuration: Duration,窗口长度/宽度
    //slideDuration: Duration,窗口滑动间隔
    //注意:windowDuration和slideDuration必须是batchDuration的倍数
    //windowDuration=slideDuration:数据不会丢失也不会重复计算==开发中会使用
    //windowDuration>slideDuration:数据会重复计算==开发中会使用
    //windowDuration<slideDuration:数据会丢失
    //代码表示:
    //windowDuration=10
    //slideDuration=5
    //那么执行结果就是每隔5s计算最近10s的数据
    //比如开发中让你统计最近1小时的数据,每隔1分钟计算一次,那么参数该如何设置?

    wordAndCount.print()
    ssc.start()//开启
    ssc.awaitTermination()//等待优雅停止

  }

}

 

Window length / width 10 and window sliding interval 5 

Published 224 original articles · 300 praises · 300,000 views

Guess you like

Origin blog.csdn.net/bbvjx1314/article/details/105428456