[英]Update concurrent map inside a stream map on flink
我有一個流,不斷地流某些鍵的最新值。
流A: DataStream[(String,Double)]
我還有另一個要在每個流程調用中獲取最新值的流。
我的方法是引入concurrentHashMap
,它將由流A更新並由第二個流讀取。
val rates = new concurrentHasMap[String,Double].asScala
val streamA : DataStream[(String,Double)]= ???
streamA.map(keyWithValue => rates(keyWithValue._1)= keyWithValue._2) //rates never gets updated
rates("testKey")=2 //this works
val streamB: DataStream[String] = ???
streamB.map(str=> rates(str) // rates does not contain the values of the streamA at this point
//some other functionality
)
是否可以從流中更新並發映射? 任何其他與流共享數據流的解決方案也是可以接受的
您嘗試使用的行為將無法以分布式方式起作用,基本上,如果您的parellelism
> 1,則該行為將不起作用。 在您的代碼rates
實際上是更新的,但是在並行運算符的不同實例中。
實際上,在這種情況下,您想要執行的操作是使用BroadcastState
,該狀態旨在完全解決您面臨的問題。
在您的特定用例中,它看起來像這樣:
val streamA : DataStream[(String,Double)]= ???
val streamABroadcasted = streamA.broadcast(<Your Map State Definition>)
val streamB: DataStream[String] = ???
streamB.connect(streamABroadcasted)
然后,您可以輕松地使用BroadcastProcessFunction
來實現您的邏輯。 可以在這里找到有關廣播狀態模式的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.