Apache flink Flink-键控进程函数中的Java类成员

Apache flink Flink-键控进程函数中的Java类成员,apache-flink,flink-streaming,Apache Flink,Flink Streaming,我有以下flink keyedprocessfunction。我基本上是在尝试实现状态设计模式 public AlertProcessor extends KeyedProcessFunction<Tuple2<String, String>, Event1, Event2> { private transient AlertState currentState; private transient AlertState activeAlertState;

我有以下flink keyedprocessfunction。我基本上是在尝试实现状态设计模式

public AlertProcessor extends KeyedProcessFunction<Tuple2<String, String>, Event1, Event2> {

   private transient AlertState currentState;
   private transient AlertState activeAlertState;
   private transient AlertState noActiveAlertState;
   private transient AlertState resolvedAlertState;

   @Override
   public void open(Configuration parameters) {
      activeAlertState = new ActiveAlertState();
      noActiveAlertState = new NoActiveAlertState();
      resolvedAlertState = new ResolvedAlertState();
   }


   @Override
   public processElement(Event1 event1, Context ctx, Collector<Event2> out) throws Exception {

        // Would the below if condition work for multiple keys?
        if (currentAlertState == null) {
            currentAlertState = noActiveAlertState;
        }

        currentAlertState.handle(event1, out);
   }

   private interface AlertState {
        void handle(Event1 event1, Collector<Event2> out);
   } 

   private class ActiveAlertState implements AlertState {
       void handle(Event1 event1, Collector<Event2> out) {
           logger.debug("Moving to no alertState");
           
           // Do something and push some Event2 to out
           currentAlertState = resolvedActiveAlertState;
       }
   }
 
   private class NoActiveAlertState implements AlertState {
       void handle(Event1 event1, Collector<Event2> out) {
           logger.debug("Moving to no alertState");
            
           // Do something and push some Event2 to out
           currentAlertState = activeAlertState;
       }
   }

   private class ResolvedAlertState implements AlertState {
       
       void handle(Event1 event1, Collector<Event2> out) {
           logger.debug("Moving to no alertState");
           
           // Do something and push some Event2 to out
           currentAlertState = noActiveAlertState;
       }
   }

}
公共AlertProcessor扩展了KeyedProcessFunction{ 私有瞬态报警状态currentState; 私有瞬态警报状态activeAlertState; 私有瞬态警报状态noActiveAlertState; 私有瞬态警报状态ResolvedAllertState; @凌驾 公共无效打开(配置参数){ activeAlertState=新的activeAlertState(); noActiveAlertState=新的noActiveAlertState(); resolvedAlertState=新的resolvedAlertState(); } @凌驾 公共processElement(Event1、Event1、上下文ctx、收集器输出)引发异常{ //以下条件是否适用于多个键? 如果(currentAlertState==null){ currentAlertState=noActiveAlertState; } currentAlertState.handle(事件1,输出); } 专用接口警报状态{ 无效句柄(Event1 Event1,收集器输出); } 私有类ActiveAlertState实现AlertState{ 无效句柄(事件1事件1,收集器输出){ 调试(“移动到无警报状态”); //做点什么,把一些事情推出去 currentAlertState=resolvedActiveAlertState; } } 私有类NoActiveAlertState实现AlertState{ 无效句柄(事件1事件1,收集器输出){ 调试(“移动到无警报状态”); //做点什么,把一些事情推出去 currentAlertState=activeAlertState; } } 私有类ResolvedAlertState实现AlertState{ 无效句柄(事件1事件1,收集器输出){ 调试(“移动到无警报状态”); //做点什么,把一些事情推出去 currentAlertState=noActiveAlertState; } } } 我的问题是-

  • 流中每个键是否有一个AlertProcessor实例(或对象)?换句话说,currentAlertState对象是否每个键都是唯一的?或者此AlertProcessor运算符的每个实例将有一个currentAlertState
  • 若currentAlertState是运算符的每个实例,那个么该代码将不会真正工作,因为currentAlertState将被不同键覆盖。我的理解正确吗

  • 我可以将currentAlertState存储在键控状态中,并为每次processElement()调用初始化它。如果这样做,我就不需要在handle()实现中将currentAlertState分配或设置为下一个状态,因为currentAlertState将根据处于flink状态的内容进行初始化

  • 有没有更好的方法在flink中实现状态设计模式,并且仍然减少创建的状态对象的数量


  • 将在管道的每个并行实例(每个任务槽)中创建一个
    AlertProcessor
    实例,该实例将在该槽处理的所有密钥上进行多路复用

    若currentAlertState是运算符的每个实例,那个么该代码将不会真正工作,因为currentAlertState将被不同键覆盖。我的理解正确吗

    对。您应该为
    currentAlertState
    使用键控状态,这将在状态后端为每个不同的键生成一个条目