Search in sources :

Example 26 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testReduceWithEvictorAndProcessFunction.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithEvictorAndProcessFunction() 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<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, new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple, TimeWindow>() {

        @Override
        public void process(Tuple tuple, 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) 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) Tuple(org.apache.flink.api.java.tuple.Tuple) EventTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger) Test(org.junit.Test)

Example 27 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testProcessProcessingTime.

@Test
@SuppressWarnings("rawtypes")
public void testProcessProcessingTime() 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(TumblingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).process(new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void process(String key, Context ctx, Iterable<Tuple2<String, Integer>> values, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (Tuple2<String, Integer> in : values) {
                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 WindowOperator);
    WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof ProcessingTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingProcessingTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ProcessingTimeTrigger(org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) TumblingProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Test(org.junit.Test)

Example 28 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testMergingAssignerWithNonMergingTriggerFails.

@Test
public void testMergingAssignerWithNonMergingTriggerFails() throws Exception {
    // verify that we check for trigger compatibility
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    WindowedStream<String, String, TimeWindow> windowedStream = env.fromElements("Hello", "Ciao").keyBy(new KeySelector<String, String>() {

        private static final long serialVersionUID = 598309916882894293L;

        @Override
        public String getKey(String value) throws Exception {
            return value;
        }
    }).window(EventTimeSessionWindows.withGap(Time.seconds(5)));
    try {
        windowedStream.trigger(new Trigger<String, TimeWindow>() {

            private static final long serialVersionUID = 6558046711583024443L;

            @Override
            public TriggerResult onElement(String element, long timestamp, TimeWindow window, TriggerContext ctx) throws Exception {
                return null;
            }

            @Override
            public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
                return null;
            }

            @Override
            public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
                return null;
            }

            @Override
            public boolean canMerge() {
                return false;
            }

            @Override
            public void clear(TimeWindow window, TriggerContext ctx) throws Exception {
            }
        });
    } catch (UnsupportedOperationException e) {
        // use a catch to ensure that the exception is thrown by the fold
        return;
    }
    fail("The trigger call should fail.");
}
Also used : TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) KeySelector(org.apache.flink.api.java.functions.KeySelector) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Test(org.junit.Test)

Example 29 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowTranslationTest method testApplyWithPreReducerEventTime.

/**
	 * Test for the deprecated .apply(Reducer, WindowFunction).
	 */
@Test
@SuppressWarnings("rawtypes")
public void testApplyWithPreReducerEventTime() 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))).apply(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 30 with TimeWindow

use of org.apache.flink.streaming.api.windowing.windows.TimeWindow in project flink by apache.

the class WindowedStream method createFastTimeOperatorIfValid.

private <R> SingleOutputStreamOperator<R> createFastTimeOperatorIfValid(InternalWindowFunction<Iterable<T>, R, K, W> function, TypeInformation<R> resultType, String functionName) {
    if (windowAssigner.getClass() == SlidingAlignedProcessingTimeWindows.class && trigger == null && evictor == null) {
        SlidingAlignedProcessingTimeWindows timeWindows = (SlidingAlignedProcessingTimeWindows) windowAssigner;
        final long windowLength = timeWindows.getSize();
        final long windowSlide = timeWindows.getSlide();
        String opName = "Fast " + timeWindows + " of " + functionName;
        @SuppressWarnings("unchecked") InternalWindowFunction<Iterable<T>, R, K, TimeWindow> timeWindowFunction = (InternalWindowFunction<Iterable<T>, R, K, TimeWindow>) function;
        OneInputStreamOperator<T, R> op = new AccumulatingProcessingTimeWindowOperator<>(timeWindowFunction, input.getKeySelector(), input.getKeyType().createSerializer(getExecutionEnvironment().getConfig()), input.getType().createSerializer(getExecutionEnvironment().getConfig()), windowLength, windowSlide);
        return input.transform(opName, resultType, op);
    } else if (windowAssigner.getClass() == TumblingAlignedProcessingTimeWindows.class && trigger == null && evictor == null) {
        TumblingAlignedProcessingTimeWindows timeWindows = (TumblingAlignedProcessingTimeWindows) windowAssigner;
        final long windowLength = timeWindows.getSize();
        final long windowSlide = timeWindows.getSize();
        String opName = "Fast " + timeWindows + " of " + functionName;
        @SuppressWarnings("unchecked") InternalWindowFunction<Iterable<T>, R, K, TimeWindow> timeWindowFunction = (InternalWindowFunction<Iterable<T>, R, K, TimeWindow>) function;
        OneInputStreamOperator<T, R> op = new AccumulatingProcessingTimeWindowOperator<>(timeWindowFunction, input.getKeySelector(), input.getKeyType().createSerializer(getExecutionEnvironment().getConfig()), input.getType().createSerializer(getExecutionEnvironment().getConfig()), windowLength, windowSlide);
        return input.transform(opName, resultType, op);
    }
    return null;
}
Also used : InternalWindowFunction(org.apache.flink.streaming.runtime.operators.windowing.functions.InternalWindowFunction) AccumulatingProcessingTimeWindowOperator(org.apache.flink.streaming.runtime.operators.windowing.AccumulatingProcessingTimeWindowOperator) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) TumblingAlignedProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingAlignedProcessingTimeWindows) SlidingAlignedProcessingTimeWindows(org.apache.flink.streaming.api.windowing.assigners.SlidingAlignedProcessingTimeWindows)

Aggregations

TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)158 Test (org.junit.Test)147 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)99 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)75 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)48 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)43 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)38 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)37 Watermark (org.apache.flink.streaming.api.watermark.Watermark)29 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)28 TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)28 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)28 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)26 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 Configuration (org.apache.flink.configuration.Configuration)22 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)19 Tuple (org.apache.flink.api.java.tuple.Tuple)13 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)12 TumblingProcessingTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows)12