Spark Streaming 是一個基於spark的實時計算框架。詳細說明參考Spark Streaming Programming Guide. 以下是編程指南WordCount例子的在spark-shell了實驗記錄。運行前,把spar/conf目錄下的log4j.properties裏的日誌級別由INFO改爲WARN,否則會打印太多的日誌。
1. 運行 nc 模擬數據源輸入
文中的命令爲 nc -lk 9999 ,運行後在另一個窗口用netstat -tnlp | grep 9999 命令檢查一下,如果沒有輸出改用nc -lk -p 9999 再試一下。
2. 運行 spark-shell
spark-shell --master spark://d-hdp-01:7077 --executor-memory 1g
在paste模式拷貝以下代碼,其中ip地址根據實際情況設置,spark-shell已創建了上下文,就不用在創建上下文了。
import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.StreamingContext._
val ssc = new StreamingContext(sc, Seconds(1))
val lines = ssc.socketTextStream("192.1.131.205", 9999)
val words = lines.flatMap(_.split(" "))
val pairs = words.map(word => (word, 1))
val wordCounts = pairs.reduceByKey(_ + _)
wordCounts.print()
ssc.start() // Start the computation
ssc.awaitTermination() // Wait for the computation to terminate
nc 中沒有輸入的的情況下
在nc中輸入數據