Search in sources :

Example 26 with SlidingEventTimeWindows

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

the class AllWindowTranslationTest method testReduceWithEvictorAndProcessFunction.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithEvictorAndProcessFunction() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DummyReducer reducer = new DummyReducer();
    DataStream<Tuple2<String, Integer>> window1 = source.windowAll(SlidingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).evictor(CountEvictor.of(100)).reduce(reducer, new ProcessAllWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow>() {

        @Override
        public void process(Context context, Iterable<Tuple2<String, Integer>> elements, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (Tuple2<String, Integer> in : elements) {
                out.collect(in);
            }
        }
    });
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof EvictingWindowOperator);
    EvictingWindowOperator<String, Tuple2<String, Integer>, ?, ?> winOperator = (EvictingWindowOperator<String, Tuple2<String, Integer>, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getEvictor() instanceof CountEvictor);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) ExpectedException(org.junit.rules.ExpectedException) CountEvictor(org.apache.flink.streaming.api.windowing.evictors.CountEvictor) Tuple2(org.apache.flink.api.java.tuple.Tuple2) 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 27 with SlidingEventTimeWindows

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

the class SlidingEventTimeWindowsTest method testWindowAssignmentWithOffset.

@Test
public void testWindowAssignmentWithOffset() {
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingEventTimeWindows assigner = SlidingEventTimeWindows.of(Time.milliseconds(5000), Time.milliseconds(1000), Time.milliseconds(100));
    assertThat(assigner.assignWindows("String", 100L, mockContext), containsInAnyOrder(timeWindow(-3900, 1100), timeWindow(-2900, 2100), timeWindow(-1900, 3100), timeWindow(-900, 4100), timeWindow(100, 5100)));
    assertThat(assigner.assignWindows("String", 5099L, mockContext), containsInAnyOrder(timeWindow(100, 5100), timeWindow(1100, 6100), timeWindow(2100, 7100), timeWindow(3100, 8100), timeWindow(4100, 9100)));
    assertThat(assigner.assignWindows("String", 5100L, mockContext), containsInAnyOrder(timeWindow(1100, 6100), timeWindow(2100, 7100), timeWindow(3100, 8100), timeWindow(4100, 9100), timeWindow(5100, 10100)));
}
Also used : WindowAssigner(org.apache.flink.streaming.api.windowing.assigners.WindowAssigner) SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) Test(org.junit.Test)

Example 28 with SlidingEventTimeWindows

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

the class SlidingEventTimeWindowsTest method testWindowAssignmentWithNegativeOffset.

@Test
public void testWindowAssignmentWithNegativeOffset() {
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingEventTimeWindows assigner = SlidingEventTimeWindows.of(Time.milliseconds(5000), Time.milliseconds(1000), Time.milliseconds(-100));
    assertThat(assigner.assignWindows("String", 0L, mockContext), containsInAnyOrder(timeWindow(-4100, 900), timeWindow(-3100, 1900), timeWindow(-2100, 2900), timeWindow(-1100, 3900), timeWindow(-100, 4900)));
    assertThat(assigner.assignWindows("String", 4899L, mockContext), containsInAnyOrder(timeWindow(-100, 4900), timeWindow(900, 5900), timeWindow(1900, 6900), timeWindow(2900, 7900), timeWindow(3900, 8900)));
    assertThat(assigner.assignWindows("String", 4900L, mockContext), containsInAnyOrder(timeWindow(900, 5900), timeWindow(1900, 6900), timeWindow(2900, 7900), timeWindow(3900, 8900), timeWindow(4900, 9900)));
}
Also used : WindowAssigner(org.apache.flink.streaming.api.windowing.assigners.WindowAssigner) SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) Test(org.junit.Test)

Example 29 with SlidingEventTimeWindows

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

the class SlidingEventTimeWindowsTest method testWindowAssignment.

@Test
public void testWindowAssignment() {
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingEventTimeWindows assigner = SlidingEventTimeWindows.of(Time.milliseconds(5000), Time.milliseconds(1000));
    assertThat(assigner.assignWindows("String", 0L, mockContext), containsInAnyOrder(timeWindow(-4000, 1000), timeWindow(-3000, 2000), timeWindow(-2000, 3000), timeWindow(-1000, 4000), timeWindow(0, 5000)));
    assertThat(assigner.assignWindows("String", 4999L, mockContext), containsInAnyOrder(timeWindow(0, 5000), timeWindow(1000, 6000), timeWindow(2000, 7000), timeWindow(3000, 8000), timeWindow(4000, 9000)));
    assertThat(assigner.assignWindows("String", 5000L, mockContext), containsInAnyOrder(timeWindow(1000, 6000), timeWindow(2000, 7000), timeWindow(3000, 8000), timeWindow(4000, 9000), timeWindow(5000, 10000)));
}
Also used : WindowAssigner(org.apache.flink.streaming.api.windowing.assigners.WindowAssigner) SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) Test(org.junit.Test)

Aggregations

SlidingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows)29 Test (org.junit.Test)29 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)24 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)24 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)21 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)21 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)12 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)11 CountEvictor (org.apache.flink.streaming.api.windowing.evictors.CountEvictor)8 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)6 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)5 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)5 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)5 WindowAssigner (org.apache.flink.streaming.api.windowing.assigners.WindowAssigner)4 CountTrigger (org.apache.flink.streaming.api.windowing.triggers.CountTrigger)4 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)2 Tuple (org.apache.flink.api.java.tuple.Tuple)2 ExpectedException (org.junit.rules.ExpectedException)2