安装命令:
yum install nc
监听端口
nc -l -k -p 6666
启动Sparkstreaming 程序
package streaming import org.apache.spark.SparkConf import org.apache.spark.streaming.dstream.DStream import org.apache.spark.streaming.{Seconds, StreamingContext} /** * spark streaming 处理socket数据 */ object NetworkWordCount { def main(args: Array[String]): Unit = { val spar: SparkConf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount") val ssc = new StreamingContext(spar,Seconds(5)) val lines = ssc.socketTextStream("192.168.100.100",6666) val result: DStream[(String, Int)] = lines.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_) result.print() ssc.start() ssc.awaitTermination() } }
在监听端口 输出单词以空格切割 ,才程序运行端查看结果