How to attach a HashMap to a Configuration object in Flink?

前端 未结 1 1777
感情败类
感情败类 2021-01-27 04:31

I want to share a HashMap across every node in Flink and allow the nodes to update that HashMap. I have this code so far:

object ParallelStreams {
          


        
相关标签:
1条回答
  • 2021-01-27 04:55

    The common way to distribute parameters to operators is to have them as regular member variables in the function class. The function object that is created and assigned during plan construction is serialized and shipped to all workers. So you don't have to pass parameters via a configuration.

    This would look as follows

    class MyMapper(map: HashMap) extends MapFunction[String, String] {
     // class definition
    }
    
    
    val inStream: DataStream[String] = ???
    
    val myHashMap: HashMap = ???
    val myMapper: MyMapper = new MyMapper(myHashMap)
    val mappedStream: DataStream[String] = inStream.map(myMapper)
    

    The myMapper object is serialized (using Java serialization) and shipped for execution. So the type of map must implement the Java Serializable interface.

    EDIT: I missed the part that you want the map to be updatable from all parallel tasks. That is not possible with Flink. You would have to either fully replicate the map and all updated (by broadcasting) or use an external system (key-value store) for that.

    0 讨论(0)
提交回复
热议问题