Search in sources :

Example 6 with ReduceSum

use of org.apache.flink.state.api.utils.ReduceSum in project flink by apache.

the class SavepointWindowReaderITCase method testReduceWindowStateReader.

@Test
public void testReduceWindowStateReader() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStateBackend(getStateBackend());
    env.setParallelism(4);
    env.addSource(createSource(numbers)).rebalance().assignTimestampsAndWatermarks(WatermarkStrategy.<Integer>noWatermarks().withTimestampAssigner((event, timestamp) -> 0)).keyBy(id -> id).window(TumblingEventTimeWindows.of(Time.milliseconds(10))).reduce(new ReduceSum()).uid(uid).addSink(new DiscardingSink<>());
    String savepointPath = takeSavepoint(env);
    SavepointReader savepoint = SavepointReader.read(env, savepointPath, getStateBackend());
    List<Integer> results = JobResultRetriever.collect(savepoint.window(TumblingEventTimeWindows.of(Time.milliseconds(10))).reduce(uid, new ReduceSum(), Types.INT, Types.INT));
    Assert.assertThat("Unexpected results from keyed state", results, Matchers.containsInAnyOrder(numbers));
}
Also used : StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ReduceSum(org.apache.flink.state.api.utils.ReduceSum) Test(org.junit.Test)

Example 7 with ReduceSum

use of org.apache.flink.state.api.utils.ReduceSum in project flink by apache.

the class SavepointWindowReaderITCase method testReduceEvictorWindowStateReader.

@Test
public void testReduceEvictorWindowStateReader() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStateBackend(getStateBackend());
    env.setParallelism(4);
    env.addSource(createSource(numbers)).rebalance().assignTimestampsAndWatermarks(WatermarkStrategy.<Integer>noWatermarks().withTimestampAssigner((event, timestamp) -> 0)).keyBy(id -> id).window(TumblingEventTimeWindows.of(Time.milliseconds(10))).evictor(new NoOpEvictor<>()).reduce(new ReduceSum()).uid(uid).addSink(new DiscardingSink<>());
    String savepointPath = takeSavepoint(env);
    SavepointReader savepoint = SavepointReader.read(env, savepointPath, getStateBackend());
    List<Integer> results = JobResultRetriever.collect(savepoint.window(TumblingEventTimeWindows.of(Time.milliseconds(10))).evictor().reduce(uid, new ReduceSum(), Types.INT, Types.INT));
    Assert.assertThat("Unexpected results from keyed state", results, Matchers.containsInAnyOrder(numbers));
}
Also used : TimestampedValue(org.apache.flink.streaming.runtime.operators.windowing.TimestampedValue) CountTrigger(org.apache.flink.streaming.api.windowing.triggers.CountTrigger) ReduceSum(org.apache.flink.state.api.utils.ReduceSum) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) PurgingTrigger(org.apache.flink.streaming.api.windowing.triggers.PurgingTrigger) JobResultRetriever(org.apache.flink.state.api.utils.JobResultRetriever) SavepointTestBase(org.apache.flink.state.api.utils.SavepointTestBase) Window(org.apache.flink.streaming.api.windowing.windows.Window) WindowReaderFunction(org.apache.flink.state.api.functions.WindowReaderFunction) StateBackend(org.apache.flink.runtime.state.StateBackend) ReducingState(org.apache.flink.api.common.state.ReducingState) Collector(org.apache.flink.util.Collector) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) ReduceFunction(org.apache.flink.api.common.functions.ReduceFunction) GlobalWindow(org.apache.flink.streaming.api.windowing.windows.GlobalWindow) Types(org.apache.flink.api.common.typeinfo.Types) Time(org.apache.flink.streaming.api.windowing.time.Time) DiscardingSink(org.apache.flink.streaming.api.functions.sink.DiscardingSink) Evictor(org.apache.flink.streaming.api.windowing.evictors.Evictor) Matchers(org.hamcrest.Matchers) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) Test(org.junit.Test) AggregateSum(org.apache.flink.state.api.utils.AggregateSum) ProcessWindowFunction(org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction) WindowFunction(org.apache.flink.streaming.api.functions.windowing.WindowFunction) List(java.util.List) TumblingEventTimeWindows(org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows) Assert(org.junit.Assert) GlobalWindows(org.apache.flink.streaming.api.windowing.assigners.GlobalWindows) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ReduceSum(org.apache.flink.state.api.utils.ReduceSum) Test(org.junit.Test)

Example 8 with ReduceSum

use of org.apache.flink.state.api.utils.ReduceSum in project flink by apache.

the class WindowReaderTest method testSessionWindow.

@Test
public void testSessionWindow() throws Exception {
    WindowOperator<Integer, Integer, ?, Void, ?> operator = getWindowOperator(stream -> stream.window(EventTimeSessionWindows.withGap(Time.milliseconds(3))).reduce(new ReduceSum()));
    OperatorState operatorState = getOperatorState(operator);
    KeyedStateInputFormat<Integer, TimeWindow, Integer> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), WindowReaderOperator.reduce(new ReduceSum(), new PassThroughReader<>(), Types.INT, new TimeWindow.Serializer(), Types.INT));
    List<Integer> list = readState(format);
    Assert.assertEquals(Collections.singletonList(2), list);
}
Also used : PassThroughReader(org.apache.flink.state.api.input.operator.window.PassThroughReader) Configuration(org.apache.flink.configuration.Configuration) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) ReduceSum(org.apache.flink.state.api.utils.ReduceSum) Test(org.junit.Test)

Aggregations

ReduceSum (org.apache.flink.state.api.utils.ReduceSum)8 Test (org.junit.Test)8 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)6 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)6 List (java.util.List)4 WatermarkStrategy (org.apache.flink.api.common.eventtime.WatermarkStrategy)4 ReduceFunction (org.apache.flink.api.common.functions.ReduceFunction)4 ReducingState (org.apache.flink.api.common.state.ReducingState)4 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)4 Types (org.apache.flink.api.common.typeinfo.Types)4 LongSerializer (org.apache.flink.api.common.typeutils.base.LongSerializer)4 StateBackend (org.apache.flink.runtime.state.StateBackend)4 WindowReaderFunction (org.apache.flink.state.api.functions.WindowReaderFunction)4 AggregateSum (org.apache.flink.state.api.utils.AggregateSum)4 SavepointTestBase (org.apache.flink.state.api.utils.SavepointTestBase)4 DiscardingSink (org.apache.flink.streaming.api.functions.sink.DiscardingSink)4 ProcessWindowFunction (org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction)4 WindowFunction (org.apache.flink.streaming.api.functions.windowing.WindowFunction)4 GlobalWindows (org.apache.flink.streaming.api.windowing.assigners.GlobalWindows)4 TumblingEventTimeWindows (org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows)4