sparkstreaming中windowoperations原语

美图欣赏:
在这里插入图片描述
一.背景

应用背景:计算的批次间隔不变,但每次展示的结果范围是多个批次间隔的范围,此时最好用SparkStreaming提供的窗口操作实现

使用:需要提供两个重要的参数:窗口长度、滑动间隔

二.代码实现

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

/**
  * 窗口操作的使用
  * 需求:批次间隔为2秒,但每次展示的结果范围为10秒,10秒展示一次
  */
object WindowOperationsDemo {
  def main(args: Array[String]): Unit = {
    // 初始化
    val conf = new SparkConf().setAppName(this.getClass.getName).setMaster("local[2]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("ERROR")
    // 实例化Streaming的上下文
    val ssc = new StreamingContext(sc, Durations.seconds(2))

    // 从NetCat服务里获取数据
    val logs: ReceiverInputDStream[String] = ssc.socketTextStream("node01", 6666)
    // 分析
    val tups = logs.flatMap(_.split(" ")).map((_, 1))
    val res: DStream[(String, Int)] =
      tups.reduceByKeyAndWindow((x: Int, y: Int) => (x + y), Seconds(10), Seconds(10))

    res.print()

    ssc.start() // 提交作业到集群
    ssc.awaitTermination() // 线程等待,等待处理任务
  }
}

        ————保持饥饿,保持学习
               Jackson_MVP

猜你喜欢

转载自blog.csdn.net/Jackson_mvp/article/details/105525170