SparkStreaming之WordCount

代码:

import org.apache.log4j.{Level, Logger}
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object StreamingWordCount {
def main(args: Array[String]): Unit = {
Logger.getLogger("org").setLevel(Level.ERROR)
val conf = new SparkConf().setMaster("local[2]").setAppName("streamingwordcount")
val sc = new StreamingContext(conf, Seconds(5))
val lines = sc.socketTextStream("localhost", 9999)
val result = lines.flatMap(_.split(" "))
.map((_, 1))
.reduceByKey(_ + _)
result.print()

sc.start()
sc.awaitTermination()
}
}

结果:

posted @ 2019-12-11 17:22  大牛和小白  阅读(348)  评论(0编辑  收藏  举报