@OnTimer窗口后未触发

时间:2019-07-03 11:36:43

标签: java google-cloud-dataflow apache-beam

我一直在使用Apache Beam的Timers,但无法触发它们。

据我所知,您可以在DoFn中以以下方式定义计时器。

@TimerId("expiry")
private final TimerSpec expirySpec = TimerSpecs.timer(TimeDomain.PROCESSING_TIME);

我选择TimeDomain.PROCESSING_TIME是因为我的事件没有分配时间戳,并且希望在Windows完成后立即触发Timer的执行。

        .apply(
             "FixedWindow",
            Window.<KV<String, GenericRecord>>into(FixedWindows.of(Duration.standardMinutes(1)))
                .triggering(AfterProcessingTime
                    .pastFirstElementInPane()
                    .plusDelayOf(Duration.standardSeconds(1)))
                .withAllowedLateness(Duration.ZERO)
                .accumulatingFiredPanes()
        )
        .apply("ExecuteAfterWindowFn", ParDo.of(new ExecuteAfterWindowFn()));

我希望下面的计时器位于DoFn中,该计时器基本上在缓冲区中累积对象,并且在完成窗口后继续执行管道并处理事件集...

        @OnTimer("expiry")
    public void onExpiry(
        OnTimerContext context,
        @StateId("bufferedSize") ValueState<Integer> bufferedSizeState,
        @StateId("buffered") BagState<GenericRecord> bufferedState) throws IOException {
        flush(context, bufferedState, bufferedSizeState);
    }

...成功执行。我是否缺少某些知识或不了解Timers如何在Apache Beam中工作?

1 个答案:

答案 0 :(得分:0)

您可以检查[1]那里有一些计时器用法的例子。

您需要设置计时器何时触发[2],这可能是错过的地方。

[1] https://beam.apache.org/blog/2017/08/28/timely-processing.html

[2] https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/state/Timer.java#L53