Search in sources :

Example 21 with AggregatingStateDescriptor

use of org.apache.flink.api.common.state.AggregatingStateDescriptor in project flink by apache.

the class AllWindowTranslationTest method testAggregateWithWindowFunctionEventTime.

@Test
public void testAggregateWithWindowFunctionEventTime() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));
    DataStream<Tuple3<String, String, Integer>> window = source.windowAll(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS))).aggregate(new DummyAggregationFunction(), new TestAllWindowFunction());
    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 AggregatingStateDescriptor);
    processElementAndEnsureOutput(operator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
Also used : TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) 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 22 with AggregatingStateDescriptor

use of org.apache.flink.api.common.state.AggregatingStateDescriptor in project flink by apache.

the class StreamingRuntimeContextTest method testAggregatingStateInstantiation.

@Test
public void testAggregatingStateInstantiation() throws Exception {
    final ExecutionConfig config = new ExecutionConfig();
    config.registerKryoType(Path.class);
    final AtomicReference<Object> descriptorCapture = new AtomicReference<>();
    StreamingRuntimeContext context = createRuntimeContext(descriptorCapture, config);
    @SuppressWarnings("unchecked") AggregateFunction<String, TaskInfo, String> aggregate = (AggregateFunction<String, TaskInfo, String>) mock(AggregateFunction.class);
    AggregatingStateDescriptor<String, TaskInfo, String> descr = new AggregatingStateDescriptor<>("name", aggregate, TaskInfo.class);
    context.getAggregatingState(descr);
    AggregatingStateDescriptor<?, ?, ?> descrIntercepted = (AggregatingStateDescriptor<?, ?, ?>) descriptorCapture.get();
    TypeSerializer<?> serializer = descrIntercepted.getSerializer();
    // check that the Path class is really registered, i.e., the execution config was applied
    assertTrue(serializer instanceof KryoSerializer);
    assertTrue(((KryoSerializer<?>) serializer).getKryo().getRegistration(Path.class).getId() > 0);
}
Also used : Path(org.apache.flink.core.fs.Path) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer) TaskInfo(org.apache.flink.api.common.TaskInfo) AggregateFunction(org.apache.flink.api.common.functions.AggregateFunction) Test(org.junit.Test)

Aggregations

AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)22 Test (org.junit.Test)17 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)11 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)10 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)8 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)6 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)6 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)6 PublicEvolving (org.apache.flink.annotation.PublicEvolving)5 RichFunction (org.apache.flink.api.common.functions.RichFunction)5 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)5 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)5 EventTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger)5 ProcessingTimeTrigger (org.apache.flink.streaming.api.windowing.triggers.ProcessingTimeTrigger)5 StreamElementSerializer (org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer)5 TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)3 TumblingProcessingTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows)3 AggregateFunction (org.apache.flink.api.common.functions.AggregateFunction)2 VoidNamespace (org.apache.flink.runtime.state.VoidNamespace)2 SlidingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows)2