直接上代码吧
说下测试思路:
该代码监控的/tmp/sparkStream/目录;
首先,创建该目录mkdir -p /tmp/sparkStream;
然后,运行spark程序;
最后,向监控目录/tmp/sparkStream/添加数据文件;
观察spark程序运行效果。
sparkStreaming import org.apache.log4j.{LoggerLevel} import org.apache.spark.SparkConf import org.apache.spark.streaming.{SecondsStreamingContext} import org.apache.spark.streaming.StreamingContext._ object HdfsWordCount { def main(args: Array[]){ Logger.getLogger("org.apache.spark").setLevel(Level.WARN) Logger.getLogger("org.apache.eclipse.jetty.server").setLevel(Level.OFF) sparkConf = SparkConf().setAppName().setMaster() ssc = StreamingContext(sparkConf()) lines = ssc.textFileStream() words = lines.flatMap(_.split()) wordCounts = words.map(x=>(x)).reduceByKey(_+_) wordCounts.print() ssc.start() ssc.awaitTermination() } }
本文出自 “一步.一步” 博客,转载请与作者联系!