Search in sources :

Example 56 with TumblingEventTimeWindows

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

the class DataStreamTest method testWindowOperatorDescription.

/**
 * Tests that verifies window operator has different name and description.
 */
@Test
public void testWindowOperatorDescription() {
    // global window
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Long> dataStream1 = env.generateSequence(0, 0).windowAll(GlobalWindows.create()).trigger(PurgingTrigger.of(CountTrigger.of(10))).reduce(new ReduceFunction<Long>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Long reduce(Long value1, Long value2) throws Exception {
            return null;
        }
    });
    // name is simplified
    assertEquals("GlobalWindows", dataStream1.getTransformation().getName());
    // description contains detail of function:
    // TriggerWindow(GlobalWindows(), ReducingStateDescriptor{name=window-contents,
    // defaultValue=null,
    // serializer=org.apache.flink.api.common.typeutils.base.LongSerializer@6af9fcb2},
    // PurgingTrigger(CountTrigger(10)), AllWindowedStream.reduce(AllWindowedStream.java:229))
    assertTrue(dataStream1.getTransformation().getDescription().contains("PurgingTrigger"));
    // keyed window
    DataStream<Long> dataStream2 = env.generateSequence(0, 0).keyBy(value -> value).window(TumblingEventTimeWindows.of(Time.milliseconds(1000))).trigger(PurgingTrigger.of(CountTrigger.of(10))).reduce(new ReduceFunction<Long>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Long reduce(Long value1, Long value2) throws Exception {
            return null;
        }
    });
    // name is simplified
    assertEquals("TumblingEventTimeWindows", dataStream2.getTransformation().getName());
    // description contains detail of function:
    // Window(TumblingEventTimeWindows(1000), PurgingTrigger, ReduceFunction$36,
    // PassThroughWindowFunction)
    assertTrue(dataStream2.getTransformation().getDescription().contains("PurgingTrigger"));
}
Also used : Tuple1(org.apache.flink.api.java.tuple.Tuple1) Tuple2(org.apache.flink.api.java.tuple.Tuple2) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) PurgingTrigger(org.apache.flink.streaming.api.windowing.triggers.PurgingTrigger) BroadcastPartitioner(org.apache.flink.streaming.runtime.partitioner.BroadcastPartitioner) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) CoFlatMapFunction(org.apache.flink.streaming.api.functions.co.CoFlatMapFunction) KeyedBroadcastProcessFunction(org.apache.flink.streaming.api.functions.co.KeyedBroadcastProcessFunction) MapFunction(org.apache.flink.api.common.functions.MapFunction) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) Assert.assertThat(org.junit.Assert.assertThat) AggregateFunction(org.apache.flink.api.common.functions.AggregateFunction) ShufflePartitioner(org.apache.flink.streaming.runtime.partitioner.ShufflePartitioner) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) ResourceSpec(org.apache.flink.api.common.operators.ResourceSpec) CustomPartitionerWrapper(org.apache.flink.streaming.runtime.partitioner.CustomPartitionerWrapper) Duration(java.time.Duration) StreamGraph(org.apache.flink.streaming.api.graph.StreamGraph) TestLogger(org.apache.flink.util.TestLogger) Function(org.apache.flink.api.common.functions.Function) Assert.fail(org.junit.Assert.fail) AssignerWithPunctuatedWatermarks(org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Method(java.lang.reflect.Method) GlobalWindow(org.apache.flink.streaming.api.windowing.windows.GlobalWindow) StringStartsWith(org.hamcrest.core.StringStartsWith) CoMapFunction(org.apache.flink.streaming.api.functions.co.CoMapFunction) KeySelector(org.apache.flink.api.java.functions.KeySelector) ForwardPartitioner(org.apache.flink.streaming.runtime.partitioner.ForwardPartitioner) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) ConnectedStreams(org.apache.flink.streaming.api.datastream.ConnectedStreams) KeyedStream(org.apache.flink.streaming.api.datastream.KeyedStream) FlatMapFunction(org.apache.flink.api.common.functions.FlatMapFunction) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) GlobalPartitioner(org.apache.flink.streaming.runtime.partitioner.GlobalPartitioner) FilterFunction(org.apache.flink.api.common.functions.FilterFunction) List(java.util.List) TypeExtractor(org.apache.flink.api.java.typeutils.TypeExtractor) BroadcastConnectedStream(org.apache.flink.streaming.api.datastream.BroadcastConnectedStream) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) Assert.assertFalse(org.junit.Assert.assertFalse) AllWindowFunction(org.apache.flink.streaming.api.functions.windowing.AllWindowFunction) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) CountTrigger(org.apache.flink.streaming.api.windowing.triggers.CountTrigger) BroadcastStream(org.apache.flink.streaming.api.datastream.BroadcastStream) AbstractUdfStreamOperator(org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Watermark(org.apache.flink.streaming.api.watermark.Watermark) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) ObjectArrayTypeInfo(org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo) DataStreamSource(org.apache.flink.streaming.api.datastream.DataStreamSource) KeyedProcessFunction(org.apache.flink.streaming.api.functions.KeyedProcessFunction) Partitioner(org.apache.flink.api.common.functions.Partitioner) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator) StreamPartitioner(org.apache.flink.streaming.runtime.partitioner.StreamPartitioner) Collector(org.apache.flink.util.Collector) ProcessOperator(org.apache.flink.streaming.api.operators.ProcessOperator) ProcessFunction(org.apache.flink.streaming.api.functions.ProcessFunction) ReduceFunction(org.apache.flink.api.common.functions.ReduceFunction) ExpectedException(org.junit.rules.ExpectedException) Nullable(javax.annotation.Nullable) Types(org.apache.flink.api.common.typeinfo.Types) DataStreamSink(org.apache.flink.streaming.api.datastream.DataStreamSink) RebalancePartitioner(org.apache.flink.streaming.runtime.partitioner.RebalancePartitioner) StreamEdge(org.apache.flink.streaming.api.graph.StreamEdge) Time(org.apache.flink.streaming.api.windowing.time.Time) GenericTypeInfo(org.apache.flink.api.java.typeutils.GenericTypeInfo) DiscardingSink(org.apache.flink.streaming.api.functions.sink.DiscardingSink) Assert.assertNotNull(org.junit.Assert.assertNotNull) SingleOutputStreamOperator(org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LegacyKeyedProcessOperator(org.apache.flink.streaming.api.operators.LegacyKeyedProcessOperator) DataStream(org.apache.flink.streaming.api.datastream.DataStream) StreamOperator(org.apache.flink.streaming.api.operators.StreamOperator) KeyGroupStreamPartitioner(org.apache.flink.streaming.runtime.partitioner.KeyGroupStreamPartitioner) Rule(org.junit.Rule) BroadcastProcessFunction(org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction) EnumTypeInfo(org.apache.flink.api.java.typeutils.EnumTypeInfo) Assert(org.junit.Assert) GlobalWindows(org.apache.flink.streaming.api.windowing.assigners.GlobalWindows) Assert.assertEquals(org.junit.Assert.assertEquals) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 57 with TumblingEventTimeWindows

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

the class WindowTranslationTest method testApplyEventTime.

// ------------------------------------------------------------------------
// Apply Translation Tests
// ------------------------------------------------------------------------
@Test
@SuppressWarnings("rawtypes")
public void testApplyEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    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(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).apply(new WindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void apply(String key, TimeWindow window, 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 EventTimeTrigger);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) 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 58 with TumblingEventTimeWindows

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

the class WindowTranslationTest method testReduceWithProcesWindowFunctionEventTime.

@Test
@SuppressWarnings("rawtypes")
public void testReduceWithProcesWindowFunctionEventTime() 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<Tuple3<String, String, Integer>> window = source.keyBy(new TupleKeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).reduce(reducer, new ProcessWindowFunction<Tuple2<String, Integer>, Tuple3<String, String, Integer>, String, TimeWindow>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void process(String key, Context ctx, 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 59 with TumblingEventTimeWindows

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

the class WindowTranslationTest method testAggregateWithWindowFunctionEventTime.

@Test
public void testAggregateWithWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Tuple3<String, String, Integer>> source = env.fromElements(Tuple3.of("hello", "hallo", 1), Tuple3.of("hello", "hallo", 2));
    DummyReducer reducer = new DummyReducer();
    DataStream<String> window = source.keyBy(new Tuple3KeySelector()).window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).aggregate(new DummyAggregationFunction(), new TestWindowFunction());
    final OneInputTransformation<Tuple3<String, String, Integer>, String> transform = (OneInputTransformation<Tuple3<String, String, Integer>, String>) window.getTransformation();
    final OneInputStreamOperator<Tuple3<String, String, Integer>, String> 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 TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof AggregatingStateDescriptor);
    processElementAndEnsureOutput(operator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple3<>("hello", "hallo", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) 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 60 with TumblingEventTimeWindows

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

the class WindowTranslationTest method testProcessWithEvictor.

@Test
@SuppressWarnings("rawtypes")
public void testProcessWithEvictor() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    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(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).trigger(CountTrigger.of(1)).evictor(TimeEvictor.of(Time.of(100, TimeUnit.MILLISECONDS))).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 EvictingWindowOperator);
    EvictingWindowOperator<String, Tuple2<String, Integer>, ?, ?> winOperator = (EvictingWindowOperator<String, Tuple2<String, Integer>, ?, ?>) operator;
    Assert.assertTrue(winOperator.getTrigger() instanceof CountTrigger);
    Assert.assertTrue(winOperator.getEvictor() instanceof TimeEvictor);
    Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
    Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);
    processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) TimeEvictor(org.apache.flink.streaming.api.windowing.evictors.TimeEvictor) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) CountTrigger(org.apache.flink.streaming.api.windowing.triggers.CountTrigger) Test(org.junit.Test)

Aggregations

TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)101 Test (org.junit.Test)101 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)79 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)76 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)76 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)73 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)55 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)43 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)37 ExpectedException (org.junit.rules.ExpectedException)30 CountTrigger (org.apache.flink.streaming.api.windowing.triggers.CountTrigger)27 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)21 WindowAssigner (org.apache.flink.streaming.api.windowing.assigners.WindowAssigner)19 TimeEvictor (org.apache.flink.streaming.api.windowing.evictors.TimeEvictor)12 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)9 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)6 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)5 Method (java.lang.reflect.Method)3 Duration (java.time.Duration)3 List (java.util.List)3