大数据系列-SPARK-STREAMING流数据queue
package com.test
import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.streaming.{Seconds, StreamingContext}
import scala.collection.mutable
object SparkStreamingQueue {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("SparkStreamingQueue").setMaster("local[*]")
val streamingContext = new StreamingContext(sparkConf, Seconds(5))
val rddQueue = new mutable.Queue[RDD[Int]]()
streamingContext.queueStream(rddQueue, oneAtATime = false)
.map((_, 1))
.reduceByKey(_ + _)
.print()
streamingContext.start()
for (k <- 0 to 5000) {
rddQueue += streamingContext.sparkContext.makeRDD(1 to 300, 10)
Thread.sleep(2000)
}
streamingContext.awaitTermination()
}
}

该博客展示了如何在Scala中使用Spark Streaming从一个可变队列中读取数据流,创建了一个每5秒处理一次数据的StreamingContext,并将队列中的RDDs推送到流处理管道中进行map和reduceByKey操作,用于聚合统计。示例代码创建了5000个RDD并间隔2秒添加到队列,演示了实时数据处理流程。
436

被折叠的 条评论
为什么被折叠?



