Search in sources :

Example 6 with SlidingEventTimeWindows

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

the class TimeWindowTranslationTest method testReduceEventTimeWindows.

@Test
@SuppressWarnings("rawtypes")
public void testReduceEventTimeWindows() 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>> window1 = source.keyBy(0).timeWindow(Time.of(1000, TimeUnit.MILLISECONDS), Time.of(100, TimeUnit.MILLISECONDS)).reduce(new DummyReducer());
    OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform1 = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
    OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator1 = transform1.getOperator();
    Assert.assertTrue(operator1 instanceof WindowOperator);
    WindowOperator winOperator1 = (WindowOperator) operator1;
    Assert.assertTrue(winOperator1.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator1.getWindowAssigner() instanceof SlidingEventTimeWindows);
    Assert.assertTrue(winOperator1.getStateDescriptor() instanceof ReducingStateDescriptor);
}
Also used : ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) 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 7 with SlidingEventTimeWindows

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

the class SlidingEventTimeWindowsTest method testTimeUnits.

@Test
public void testTimeUnits() {
    // sanity check with one other time unit
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingEventTimeWindows assigner = SlidingEventTimeWindows.of(Time.seconds(5), Time.seconds(1), Time.milliseconds(500));
    assertThat(assigner.assignWindows("String", 100L, mockContext), containsInAnyOrder(timeWindow(-4500, 500), timeWindow(-3500, 1500), timeWindow(-2500, 2500), timeWindow(-1500, 3500), timeWindow(-500, 4500)));
    assertThat(assigner.assignWindows("String", 5499L, mockContext), containsInAnyOrder(timeWindow(500, 5500), timeWindow(1500, 6500), timeWindow(2500, 7500), timeWindow(3500, 8500), timeWindow(4500, 9500)));
    assertThat(assigner.assignWindows("String", 5100L, mockContext), containsInAnyOrder(timeWindow(500, 5500), timeWindow(1500, 6500), timeWindow(2500, 7500), timeWindow(3500, 8500), timeWindow(4500, 9500)));
}
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 8 with SlidingEventTimeWindows

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

the class SlidingEventTimeWindowsTest method testProperties.

@Test
public void testProperties() {
    SlidingEventTimeWindows assigner = SlidingEventTimeWindows.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 : SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) 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 9 with SlidingEventTimeWindows

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

the class WindowTranslationTest method testAggregateWithEvictor.

@Test
public void testAggregateWithEvictor() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Tuple3<String, String, Integer>> source = env.fromElements(Tuple3.of("hello", "hallo", 1), Tuple3.of("hello", "hallo", 2));
    DataStream<Integer> window1 = source.keyBy(new Tuple3KeySelector()).window(SlidingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).evictor(CountEvictor.of(100)).aggregate(new DummyAggregationFunction());
    final OneInputTransformation<Tuple3<String, String, Integer>, Integer> transform = (OneInputTransformation<Tuple3<String, String, Integer>, Integer>) window1.getTransformation();
    final OneInputStreamOperator<Tuple3<String, String, Integer>, Integer> operator = transform.getOperator();
    Assert.assertTrue(operator instanceof WindowOperator);
    WindowOperator<String, Tuple3<String, String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple3<String, String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple3<>("hello", "hallo", 1));
}
Also used : SlidingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) 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 SlidingEventTimeWindows

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

the class WindowTranslationTest method testReduceWithEvictor.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithEvictor() 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.keyBy(0).window(SlidingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).evictor(CountEvictor.of(100)).reduce(reducer);
    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) 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)

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