最近在使用Spark Streaming過程中,對foreachRDD有點疑問,查閱資料後記錄如下:
foreachRDD(func)的官方解釋為
The most generic output operator that applies a function, func, to each RDD generated from the stream. This function should push the data in each RDD to an external system, such as saving the RDD to files, or writing it over the network to a database. Note that the function func is executed in the driver process running the streaming application, and will usually have RDD actions in it that will force the computation of the streaming RDDs.
對于這個定義會産生一個疑問:在一個batch interval裡面會産生幾個RDD?
結論:有且隻有一個。
DStream可以了解為是基于時間的,即每個interval産生一個RDD,是以如果以時間為軸,每隔一段時間就會産生一個RDD,那麼定義中的“each RDD”應該了解為每個interval的RDD,而不是一個interval中的每個RDD。
DStream中的foreachRDD方法最終會調用如下的代碼
可以看到這個方法裡面并沒有任何的Iterator,可以對比一下RDD中的<code>foreachPartition</code>和<code>foreach</code>方法,這兩個方法是會周遊RDD,是以才會有Iterator類型的引用
而如果每個interval中有多個RDD,那麼DStream中的foreachRDD也一定會有Iterator類型的引用,但是從上述的代碼中并沒有。
作者:Woople
連結:http://www.jianshu.com/p/9116043b0c21