spark stream初探
spark帶了一個NetworkWordCount測試程式,用以統計來自某TCP連線的單詞輸入:
/usr/local/spark/bin/run-example streaming.NetworkWordCount localhost 9999
再啟動netcat:
nc -lk 9999
嘗試輸入一些單詞:
hello world
damn it
可以看到NetworkWordCount產生如下輸出:
-------------------------------------------
Time: 1425866862000 ms
-------------------------------------------
(world,1)
(hello,1)
-------------------------------------------
Time: 1425866877000 ms
-------------------------------------------
(damn,1)
(it,1)
也可以手動在shell裡輸入NetworkWordCount的程式碼:
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.StreamingContext._
// Create a local StreamingContext with two working thread and batch interval of 1 second.
// The master requires 2 cores to prevent from a starvation scenario.
val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
val ssc = new StreamingContext(conf, Seconds(1))
// Create a DStream that will connect to hostname:port, like localhost:9999
val lines = ssc.socketTextStream("localhost", 9999)
// Split each line into words
val words = lines.flatMap(_.split(" "))
val pairs = words.map(word => (word, 1))
val wordCounts = pairs.reduceByKey(_ + _)
// Print the first ten elements of each RDD generated in this DStream to the console
wordCounts.print()
ssc.start() // Start the computation
ssc.awaitTermination() // Wait for the computation to terminate
執行後,即可在螢幕上得到類似的輸出。
相關文章
- 初探Node中的stream
- Spark Stream 簡單例子Spark單例
- spark與kafaka整合workcount示例 spark-stream-kafkaSparkKafka
- Spark菜鳥之路(2):IntelLij初探SparkIntelliJ
- Spark-stream基礎---sparkStreaming和Kafka整合wordCount單詞計數SparkKafka
- Java-stream(1) Stream基本概念 & Stream介面Java
- stream
- 使用dataX-stream2stream/stream2mysql/mysql2mysql/mysql2streamMySql
- Java StreamJava
- Stream APIAPI
- elysia stream
- Stream流
- Spark之spark shellSpark
- 【Spark篇】---Spark初始Spark
- Spark on Yarn 和Spark on MesosSparkYarn
- Spark系列 - (3) Spark SQLSparkSQL
- Stream 流模組
- Java 8 StreamJava
- ORACLE STREAM ERROROracleError
- restart oracle streamRESTOracle
- Java Lambda StreamJava
- [Java]Stream用法Java
- Oracle simple streamOracle
- centos stream 8CentOS
- 聊聊 Redis StreamRedis
- Stream流求和
- Stream瞭解
- Spark學習進度-Spark環境搭建&Spark shellSpark
- 【Spark】Spark容錯機制Spark
- Spark導論(Spark自學一)Spark
- Deno 初探
- ELK初探
- Nginx 初探Nginx
- Mobx 初探
- jQuery初探jQuery
- 初探IndexedDBIndex
- 初探PWA
- Puppeteer 初探