代码:
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()
}
}
结果:
有帮助的欢迎评论打赏哈,谢谢!