Apache Flink-如果在x分钟内未收到任何数据,则发送事件

mad*_*nce 4 timer complex-event-processing data-stream apache-flink

当在一定时间内未从流中接收到任何数据时,我该如何使用Flink的DataStream API实现一个操作符来发送事件?

Fab*_*ske 10

这样的运算符可以使用来实现ProcessFunction

DataStream<Long> input = env.fromElements(1L, 2L, 3L, 4L);

input
  // use keyBy to have keyed state. 
  // NullByteKeySelector will move all data to one task. You can also use other keys
  .keyBy(new NullByteKeySelector())
  // use process function with 60 seconds timeout
  .process(new TimeOutFunction(60 * 1000));
Run Code Online (Sandbox Code Playgroud)

TimeOutFunction定义如下。在此示例中,它使用处理时间。

public static class TimeOutFunction extends ProcessFunction<Long, Boolean> {

  // delay after which an alert flag is thrown
  private final long timeOut;
  // state to remember the last timer set
  private transient ValueState<Long> lastTimer;

  public TimeOutFunction(long timeOut) {
    this.timeOut = timeOut;
  }

  @Override
  public void open(Configuration conf) {
    // setup timer state
    ValueStateDescriptor<Long> lastTimerDesc = 
      new ValueStateDescriptor<Long>("lastTimer", Long.class);
    lastTimer = getRuntimeContext().getState(lastTimerDesc);
  }

  @Override
  public void processElement(Long value, Context ctx, Collector<Boolean> out) throws Exception {
    // get current time and compute timeout time
    long currentTime = ctx.timerService().currentProcessingTime();
    long timeoutTime = currentTime + timeOut;
    // register timer for timeout time
    ctx.timerService().registerProcessingTimeTimer(timeoutTime);
    // remember timeout time
    lastTimer.update(timeoutTime);
  }

  @Override
  public void onTimer(long timestamp, OnTimerContext ctx, Collector<Boolean> out) throws Exception {
    // check if this was the last timer we registered
    if (timestamp == lastTimer.value()) {
      // it was, so no data was received afterwards.
      // fire an alert.
      out.collect(true);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 一个小周。如果流至少接收一次数据,则此设置很好。任何检测方式,如果流根本没有接收到数据。一次都没有? (2认同)
  • 这并不能回答问题。仅当流程函数至少接收一次元素时,这才有效,因为计时器只能在“processElement”中创建。 (2认同)