Search in sources :

Example 6 with SlidingProcessingTimeWindows

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

the class AllWindowTranslationTest method testReduceProcessingTime.

@Test
@SuppressWarnings("rawtypes")
public void testReduceProcessingTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple2<String, Integer>> window1 = source.windowAll(SlidingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).reduce(new DummyReducer());
    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 WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof ProcessingTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingProcessingTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ReducingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) ProcessingTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows) Test(org.junit.Test)

Example 7 with SlidingProcessingTimeWindows

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

the class SlidingProcessingTimeWindowsTest method testWindowAssignmentWithOffset.

@Test
public void testWindowAssignmentWithOffset() {
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingProcessingTimeWindows assigner = SlidingProcessingTimeWindows.of(Time.milliseconds(5000), Time.milliseconds(1000), Time.milliseconds(100));
    when(mockContext.getCurrentProcessingTime()).thenReturn(100L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(-3900, 1100), timeWindow(-2900, 2100), timeWindow(-1900, 3100), timeWindow(-900, 4100), timeWindow(100, 5100)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(5099L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(100, 5100), timeWindow(1100, 6100), timeWindow(2100, 7100), timeWindow(3100, 8100), timeWindow(4100, 9100)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(5100L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, 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) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows) Test(org.junit.Test)

Example 8 with SlidingProcessingTimeWindows

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

the class SlidingProcessingTimeWindowsTest method testProperties.

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

Example 9 with SlidingProcessingTimeWindows

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

the class SlidingProcessingTimeWindowsTest method testTimeUnits.

@Test
public void testTimeUnits() {
    // sanity check with one other time unit
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingProcessingTimeWindows assigner = SlidingProcessingTimeWindows.of(Time.seconds(5), Time.seconds(1), Time.milliseconds(500));
    when(mockContext.getCurrentProcessingTime()).thenReturn(100L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(-4500, 500), timeWindow(-3500, 1500), timeWindow(-2500, 2500), timeWindow(-1500, 3500), timeWindow(-500, 4500)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(5499L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(500, 5500), timeWindow(1500, 6500), timeWindow(2500, 7500), timeWindow(3500, 8500), timeWindow(4500, 9500)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(5100L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, 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) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows) Test(org.junit.Test)

Example 10 with SlidingProcessingTimeWindows

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

the class SlidingProcessingTimeWindowsTest method testWindowAssignment.

@Test
public void testWindowAssignment() {
    WindowAssigner.WindowAssignerContext mockContext = mock(WindowAssigner.WindowAssignerContext.class);
    SlidingProcessingTimeWindows assigner = SlidingProcessingTimeWindows.of(Time.milliseconds(5000), Time.milliseconds(1000));
    when(mockContext.getCurrentProcessingTime()).thenReturn(0L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(-4000, 1000), timeWindow(-3000, 2000), timeWindow(-2000, 3000), timeWindow(-1000, 4000), timeWindow(0, 5000)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(4999L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, mockContext), containsInAnyOrder(timeWindow(0, 5000), timeWindow(1000, 6000), timeWindow(2000, 7000), timeWindow(3000, 8000), timeWindow(4000, 9000)));
    when(mockContext.getCurrentProcessingTime()).thenReturn(5000L);
    assertThat(assigner.assignWindows("String", Long.MIN_VALUE, 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) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows) Test(org.junit.Test)

Aggregations

SlidingProcessingTimeWindows (org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows)11 Test (org.junit.Test)10 ProcessingTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger)7 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)6 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)6 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)6 WindowAssigner (org.apache.flink.streaming.api.windowing.assigners.WindowAssigner)3 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)2 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)2 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)2 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 TumblingProcessingTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows)1 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)1