Search in sources :

Example 6 with TumblingEventTimeWindows

use of org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows in project flink by apache.

the class AllWindowTranslationTest method testFoldWithProcessAllWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testFoldWithProcessAllWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple2<String, Integer>> window = source.windowAll(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).fold(new Tuple3<>("", "", 0), new DummyFolder(), new ProcessAllWindowFunction<Tuple3<String, String, Integer>, Tuple2<String, Integer>, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void process(Context ctx, Iterable<Tuple3<String, String, Integer>> values, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (Tuple3<String, String, Integer> in : values) {
                out.collect(new Tuple2<>(in.f0, in.f2));
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof FoldingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 7 with TumblingEventTimeWindows

use of org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows in project flink by apache.

the class TumblingEventTimeWindowsTest method testProperties.

@Test
public void testProperties() {
    TumblingEventTimeWindows assigner = TumblingEventTimeWindows.of(Time.seconds(5), Time.milliseconds(100));
    assertTrue(assigner.isEventTime());
    assertEquals(new TimeWindow.Serializer(), assigner.getWindowSerializer(new ExecutionConfig()));
    assertThat(assigner.getDefaultTrigger(mock(StreamExecutionEnvironment.class)), instanceOf(EventTimeTrigger.class));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 8 with TumblingEventTimeWindows

use of org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows in project flink by apache.

the class WindowTranslationTest method testFoldWithProcessWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testFoldWithProcessWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple2<String, Integer>> window = source.keyBy(new TupleKeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).fold(new Tuple3<>("", "", 0), new DummyFolder(), new ProcessWindowFunction<Tuple3<String, String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void process(String key, Context ctx, Iterable<Tuple3<String, String, Integer>> values, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (Tuple3<String, String, Integer> in : values) {
                out.collect(new Tuple2<>(in.f0, in.f2));
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof FoldingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 9 with TumblingEventTimeWindows

use of org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows in project flink by apache.

the class WindowTranslationTest method testReduceWithWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DummyReducer reducer = new DummyReducer();
    DataStream<Tuple3<String, String, Integer>> window = source.keyBy(new TupleKeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).reduce(reducer, new WindowFunction<Tuple2<String, Integer>, Tuple3<String, String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void apply(String key, TimeWindow window, Iterable<Tuple2<String, Integer>> values, Collector<Tuple3<String, String, Integer>> out) throws Exception {
            for (Tuple2<String, Integer> in : values) {
                out.collect(new Tuple3<>(in.f0, in.f0, in.f1));
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple3<String, String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple3<String, String, Integer>>) window.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple3<String, String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ReducingStateDescriptor);
    processElementAndEnsureOutput(operator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 10 with TumblingEventTimeWindows

use of org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows in project flink by apache.

the class WindowOperatorTest method testCleanupTimeOverflow.

@Test
public void testCleanupTimeOverflow() throws Exception {
    final int WINDOW_SIZE = 1000;
    final long LATENESS = 2000;
    TypeInformation<Tuple2<String, Integer>> inputType = TypeInfoParser.parse("Tuple2<String, Integer>");
    ReducingStateDescriptor<Tuple2<String, Integer>> stateDesc = new ReducingStateDescriptor<>("window-contents", new SumReducer(), inputType.createSerializer(new ExecutionConfig()));
    TumblingEventTimeWindows windowAssigner = TumblingEventTimeWindows.of(Time.milliseconds(WINDOW_SIZE));
    final WindowOperator<String, Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow> operator = new WindowOperator<>(windowAssigner, new TimeWindow.Serializer(), new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), stateDesc, new InternalSingleValueWindowFunction<>(new PassThroughWindowFunction<String, TimeWindow, Tuple2<String, Integer>>()), EventTimeTrigger.create(), LATENESS, null);
    OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple2<String, Integer>> testHarness = new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector(), BasicTypeInfo.STRING_TYPE_INFO);
    testHarness.open();
    ConcurrentLinkedQueue<Object> expected = new ConcurrentLinkedQueue<>();
    long timestamp = Long.MAX_VALUE - 1750;
    Collection<TimeWindow> windows = windowAssigner.assignWindows(new Tuple2<>("key2", 1), timestamp, new WindowAssigner.WindowAssignerContext() {

        @Override
        public long getCurrentProcessingTime() {
            return operator.windowAssignerContext.getCurrentProcessingTime();
        }
    });
    TimeWindow window = Iterables.getOnlyElement(windows);
    testHarness.processElement(new StreamRecord<>(new Tuple2<>("key2", 1), timestamp));
    // the garbage collection timer would wrap-around
    Assert.assertTrue(window.maxTimestamp() + LATENESS < window.maxTimestamp());
    // and it would prematurely fire with watermark (Long.MAX_VALUE - 1500)
    Assert.assertTrue(window.maxTimestamp() + LATENESS < Long.MAX_VALUE - 1500);
    // if we don't correctly prevent wrap-around in the garbage collection
    // timers this watermark will clean our window state for the just-added
    // element/window
    testHarness.processWatermark(new Watermark(Long.MAX_VALUE - 1500));
    // this watermark is before the end timestamp of our only window
    Assert.assertTrue(Long.MAX_VALUE - 1500 < window.maxTimestamp());
    Assert.assertTrue(window.maxTimestamp() < Long.MAX_VALUE);
    // push in a watermark that will trigger computation of our window
    testHarness.processWatermark(new Watermark(window.maxTimestamp()));
    expected.add(new Watermark(Long.MAX_VALUE - 1500));
    expected.add(new StreamRecord<>(new Tuple2<>("key2", 1), window.maxTimestamp()));
    expected.add(new Watermark(window.maxTimestamp()));
    TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expected, testHarness.getOutput(), new Tuple2ResultSortComparator());
    testHarness.close();
}
Also used : ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) KeyedOneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WindowAssigner(org.apache.flink.streaming.api.windowing.assigners.WindowAssigner) PassThroughWindowFunction(org.apache.flink.streaming.api.functions.windowing.PassThroughWindowFunction) Tuple2(org.apache.flink.api.java.tuple.Tuple2) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Aggregations

TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)35 Test (org.junit.Test)35 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)31 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)30 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)30 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)29 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)23 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)17 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)15 CountTrigger (org.apache.flink.streaming.api.windowing.triggers.CountTrigger)8 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)7 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)6 WindowAssigner (org.apache.flink.streaming.api.windowing.assigners.WindowAssigner)4 TimeEvictor (org.apache.flink.streaming.api.windowing.evictors.TimeEvictor)4 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Tuple (org.apache.flink.api.java.tuple.Tuple)1 PassThroughWindowFunction (org.apache.flink.streaming.api.functions.windowing.PassThroughWindowFunction)1