如何使用Flink的DataStream API实现一个运算符,该API在一段时间内没有从流中收到数据时发送事件?
答案 0 :(得分:5)
可以使用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));
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);
}
}
}
答案 1 :(得分:0)
您可以使用自定义触发功能设置时间窗口。在触发器功能中,每次接收到事件时,“onEvent”方法会将processingTimeTrigger设置为“currentTime + desiredTimeDelay”。然后,当新事件发生时,您删除先前设置的触发器并创建一个新触发器。如果系统时间是processTimeTrigger上的时间,则事件未到来,它将触发并且将处理窗口。即使没有事件发生,将要处理的事件列表也将是空的。