Search in sources :

Example 1 with SlidingProcessingTimeWindows

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

the class WindowTranslationTest 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.keyBy(new TupleKeySelector()).window(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 2 with SlidingProcessingTimeWindows

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

the class WindowTranslationTest method testFoldProcessingTime.

@Test
@SuppressWarnings("rawtypes")
public void testFoldProcessingTime() 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<Tuple3<String, String, Integer>> window = source.keyBy(new TupleKeySelector()).window(SlidingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).fold(new Tuple3<>("", "", 0), new DummyFolder());
    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 ProcessingTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingProcessingTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof FoldingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : ProcessingTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger) FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) 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) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows) Test(org.junit.Test)

Example 3 with SlidingProcessingTimeWindows

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

the class WindowOperator method getPaneSize.

private long getPaneSize() {
    Preconditions.checkArgument(legacyWindowOperatorType == LegacyWindowOperatorType.FAST_ACCUMULATING || legacyWindowOperatorType == LegacyWindowOperatorType.FAST_AGGREGATING);
    final long paneSlide;
    if (windowAssigner instanceof SlidingProcessingTimeWindows) {
        SlidingProcessingTimeWindows timeWindows = (SlidingProcessingTimeWindows) windowAssigner;
        paneSlide = ArithmeticUtils.gcd(timeWindows.getSize(), timeWindows.getSlide());
    } else {
        TumblingProcessingTimeWindows timeWindows = (TumblingProcessingTimeWindows) windowAssigner;
        // this is valid as windowLength == windowSlide == timeWindows.getSize
        paneSlide = timeWindows.getSize();
    }
    return paneSlide;
}
Also used : TumblingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows) SlidingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows)

Example 4 with SlidingProcessingTimeWindows

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

the class AllWindowTranslationTest method testAggregateProcessingTime.

@Test
public void testAggregateProcessingTime() 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))).aggregate(new DummyAggregationFunction());
    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 AggregatingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) 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 5 with SlidingProcessingTimeWindows

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

the class AllWindowTranslationTest method testFoldProcessingTime.

@Test
@SuppressWarnings("rawtypes")
public void testFoldProcessingTime() 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<Tuple3<String, String, Integer>> window = source.windowAll(SlidingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS))).fold(new Tuple3<>("", "", 0), new DummyFolder());
    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 ProcessingTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingProcessingTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof FoldingStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : ProcessingTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger) FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) 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) 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