新聞中心
本期內(nèi)容:
白水網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),白水網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為白水上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的白水做網(wǎng)站的公司定做!
1、updateStateByKey解密
2、mapWithState解密
背景:
整個(gè)Spark Streaming是按照Batch Duractions劃分Job的。但是很多時(shí)候我們需要算過(guò)去的一天甚至一周的數(shù)據(jù),這個(gè)時(shí)候不可避免的要進(jìn)行狀態(tài)管理,而Spark Streaming每個(gè)Batch Duractions都會(huì)產(chǎn)生一個(gè)Job,Job里面都是RDD,
所以此時(shí)面臨的問(wèn)題就是怎么對(duì)狀態(tài)進(jìn)行維護(hù)?這個(gè)時(shí)候就需要借助updateStateByKey和mapWithState方法完成核心的步驟。
1、簡(jiǎn)單看下updateStateByKey源碼:
在DStream中updateStateByKey和mapWithState是通過(guò)隱式轉(zhuǎn)換來(lái)完成,本身沒(méi)有這樣的方法。
implicit def toPairDStreamFunctions[K, V](stream: DStream[(K, V)]) (implicit kt: ClassTag[K], vt: ClassTag[V], ord: Ordering[K] = null): PairDStreamFunctions[K, V] = { new PairDStreamFunctions[K, V](stream) }
[: ClassTag]( updateFunc: ([]Option[]) => Option[] ): DStream[()] = ssc.withScope { updateStateByKey(updateFuncdefaultPartitioner()) }
最終會(huì)通過(guò)StateDStream中的computeUsingPreviousRDD和compute來(lái)完成這樣的功能,簡(jiǎn)單的流程圖如下:
2、簡(jiǎn)單看下mapWithState源碼
mapWithState是返回MapWithStateDStream對(duì)象,維護(hù)和更新歷史狀態(tài)都是基于Key,使用一個(gè)function對(duì)key-value形式的數(shù)據(jù)進(jìn)行狀態(tài)維護(hù)
[: ClassTag: ClassTag]( spec: StateSpec[] ): MapWithStateDStream[] = { MapWithStateDStreamImpl[]( selfspec.asInstanceOf[StateSpecImpl[]] ) }
通過(guò)InternalMapWithStateDStream類中的compute來(lái)完成,簡(jiǎn)單的流程圖如下:
本文題目:(版本定制)第14課:SparkStreaming源碼解讀之State管理之updateStateByKey和mapWithState解密
本文地址:http://www.dlmjj.cn/article/pesdgj.html