2025/1/10

import org.apache.spark.streaming.{Seconds, StreamingContext}

// 初始化StreamingContext
val conf = new SparkConf().setAppName("Spark Streaming").setMaster("local")
val ssc = new StreamingContext(conf, Seconds(5))

// 创建DStream
val lines = ssc.socketTextStream("localhost", 9999)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(word => (word, 1)).reduceByKey(_ + _)
wordCounts.print()

ssc.start()
ssc.awaitTermination()

posted @ 2025-01-10 20:23  为20岁努力  阅读(5)  评论(0)    收藏  举报