1. 程式人生 > >Spark Streaming 第一部分

Spark Streaming 第一部分

 Spark 概述

Spark Streaming是基於Spark core API的擴充套件,支援高吞吐,可擴充套件,容錯的。

Spark Streaming is an extension of the core Spark API that enables scalable, high-throughput, fault-tolerant stream processing of live data streams. 

 特點如下圖所示

一方面可以對接到多種資料來源,另外一方面處理完的資料可以寫到很多地方去

Spark Streaming

總結:

  • 將不同的資料來源的資料經過~的處理後將結果輸出到外部系統中
  • 低延時
  • 能能錯誤中高效的恢復,即容錯性很高
  • 能夠執行在成百上千的節點
  • 能夠將批處理、機器學習、圖計算等子框架和spark streaming綜合使用
Internally, it works as follows. 
Spark Streaming receives live input data streams and divides the data into batches, 
which are then processed by the Spark engine to generate the final stream of results in batches.

 事實上,他們是以從節點的方式工作的。Spark Streaming接收來自資料來源的資料,將他們切分成一個一個的資料塊後,傳送給Spark engine進行處理,來得到最後處理的結果

Spark Streaming

Spark提供了一站式解決方案

One stack to rule them all:一站式解決

整合Spark 生態系統的使用

Join data streams with static data sets

Combine machine learning with Streaming processing

Combine SQL with Streaming Processing Interactively query streaming data with sql

Spark Streaming的發展史

Spark 詞頻統計

org.apache.spark.examples.streaming.NetworkWordCount

核心程式碼,可以看出,Spark SQL和Spark Streaming是可以進行無縫銜接的。

    val ssc = new StreamingContext(sparkConf, Seconds(1))    
    val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
    wordCounts.print()
    ssc.start()
    ssc.awaitTermination()

 開啟NC服務

[[email protected] ~]$ nc -lk 9999

執行spark-submit 

./spark-submit --master local[2] \
--class org.apache.spark.examples.streaming.NetworkWordCount \
--name NetworkWordCount \
/home/hadoop/app/spark-2.1.0-bin-2.6.0-cdh5.7.0/examples/jars/spark-examples_2.11-2.1.0.jar hadoop000 9999

執行spark-shell

[[email protected] bin]$ ./spark-shell --master local[2]
import org.apache.spark.streaming.{Seconds, StreamingContext}
val ssc = new StreamingContext(sc, Seconds(1))    
val lines = ssc.socketTextStream("hadoop000", 9999)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
wordCounts.print()
ssc.start()
ssc.awaitTermination()

Spark的工作原理

Spark Streaming接收到資料後,按照設定的秒數來拆分,然後交由Spark引擎處理。產生processed result

執行Spark Streaming 的時候,會把資料拆分成一系列的小的確定的批次來進行處理,Spark Streaming會

把每個批次當作RDD來處理,然後交由spark引擎進行處理,最後分次分批返回。

總結:Spark Streaming接收到實時資料流,會把資料流按照指定時間段切成一片片小的資料塊,然後把小的資料塊傳給spark engine

細粒度

對於應用程式在跑的時候,會生成StreamingContext和SparkContext  Driver會生成一個receivers用來跑在worder節點的Executors上  也可以看成是StreamingContext在Executore生成的Receivers的任務    receiver起來後,等待資料來源的輸入,並且將資料來源分離成一個個資料塊,其實就是RDD  基於Executor的memory的儲存備份(可以配置)  把Blocks的副本儲存到其他的Executor上去

 Executor將接收(儲存到記憶體)中的資料的儲存(block metastore資訊地址)報告給StreamingContext    StreamingContext對每個資料塊按照RDD處理,將資料集給SparkContext按照一個個Job處理    SparkContext跑executor memory中的資料跑一個個任務    會不停、迴圈的處理