Search in sources :

Example 16 with TimerSpec

use of org.apache.beam.sdk.state.TimerSpec in project beam by apache.

the class ParDoTest method testEventTimeTimerAbsolute.

/**
   * Tests that an event time timer set absolutely for the last possible moment fires and results in
   * supplementary output. The test is otherwise identical to {@link #testEventTimeTimerBounded()}.
   */
@Test
@Category({ ValidatesRunner.class, UsesTimersInParDo.class })
public void testEventTimeTimerAbsolute() throws Exception {
    final String timerId = "foo";
    DoFn<KV<String, Integer>, Integer> fn = new DoFn<KV<String, Integer>, Integer>() {

        @TimerId(timerId)
        private final TimerSpec spec = TimerSpecs.timer(TimeDomain.EVENT_TIME);

        @ProcessElement
        public void processElement(ProcessContext context, @TimerId(timerId) Timer timer, BoundedWindow window) {
            timer.set(window.maxTimestamp());
            context.output(3);
        }

        @OnTimer(timerId)
        public void onTimer(OnTimerContext context) {
            context.output(42);
        }
    };
    PCollection<Integer> output = pipeline.apply(Create.of(KV.of("hello", 37))).apply(ParDo.of(fn));
    PAssert.that(output).containsInAnyOrder(3, 42);
    pipeline.run();
}
Also used : OnTimer(org.apache.beam.sdk.transforms.DoFn.OnTimer) Timer(org.apache.beam.sdk.state.Timer) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) StringUtils.byteArrayToJsonString(org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString) Matchers.containsString(org.hamcrest.Matchers.containsString) KV(org.apache.beam.sdk.values.KV) TimerSpec(org.apache.beam.sdk.state.TimerSpec) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

TimerSpec (org.apache.beam.sdk.state.TimerSpec)16 Test (org.junit.Test)16 Timer (org.apache.beam.sdk.state.Timer)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 StringUtils.byteArrayToJsonString (org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString)12 KV (org.apache.beam.sdk.values.KV)12 OnTimer (org.apache.beam.sdk.transforms.DoFn.OnTimer)11 Category (org.junit.experimental.categories.Category)11 Instant (org.joda.time.Instant)8 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)7 DoFn (org.apache.beam.sdk.transforms.DoFn)4 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)4 StateSpec (org.apache.beam.sdk.state.StateSpec)2 ValueState (org.apache.beam.sdk.state.ValueState)2 ArrayList (java.util.ArrayList)1 FlinkPipelineOptions (org.apache.beam.runners.flink.FlinkPipelineOptions)1 DoFnOperator (org.apache.beam.runners.flink.translation.wrappers.streaming.DoFnOperator)1 WindowParameter (org.apache.beam.sdk.transforms.reflect.DoFnSignature.Parameter.WindowParameter)1 FakeDoFn (org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.FakeDoFn)1 SlidingWindows (org.apache.beam.sdk.transforms.windowing.SlidingWindows)1