我实现了一个RichFunction具有以下结构的 Flink:
public class MyFunction extends KeyedBroadcastProcessFunction <String, InputType, BroadcastedStateType, OutputType> {
private MapState<String, MyState> myState;
@Override
public void open(Configuration conf)throws Exception{
myState = getRuntimeContext().getMapState(new MapStateDescriptor<>("state", Types.STRING, Types.POJO(BroadcastedStateType.class)));
}
@Override
public void processElement(InputType value, ReadOnlyContext ctx, Collector<OutputType> out) throws Exception {
MyState state = myState.get(value.ID());
// Do things
}
@Override
public void processBroadcastElement(BroadcastedStateType value, Context ctx, Collector<OutputType> out) throws Exception {
state.put(value.ID(), value.state()); // Update the mapState with value from broadcast
}
// retrieve all the state values and put them in the MapState
private void initialState() throws Exception{
Map<String, MyState> initialValues = ...;
this.cameras.putAll(initialValues);
}
}
该mapState变量存储通过BroadcastedStream. 更新是在processBroadcastElement()函数中完成的。
在作业开始时,我想mapState使用该initialState()函数来初始化。
问题是我无法在函数中使用它open()
(请参阅此处原因)
在这种情况下初始化的正确方法是什么mapState?(在所有使用 RichFunctions 的情况下)
子衿沉夜
梵蒂冈之花
相关分类