Search in sources :

Example 1 with ReduceSum

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

the class SavepointWindowReaderITCase method testWindowTriggerStateReader.

@Test
public void testWindowTriggerStateReader() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStateBackend(getStateBackend());
    env.setParallelism(4);
    env.addSource(createSource(numbers)).rebalance().keyBy(id -> id).window(GlobalWindows.create()).trigger(PurgingTrigger.of(CountTrigger.of(10))).reduce(new ReduceSum()).uid(uid).addSink(new DiscardingSink<>());
    String savepointPath = takeSavepoint(env);
    SavepointReader savepoint = SavepointReader.read(env, savepointPath, getStateBackend());
    List<Long> results = JobResultRetriever.collect(savepoint.window(new GlobalWindow.Serializer()).reduce(uid, new ReduceSum(), new TriggerReaderFunction(), Types.INT, Types.INT, Types.LONG));
    Assert.assertThat("Unexpected results from trigger state", results, Matchers.contains(1L, 1L, 1L));
}
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) GlobalWindow(org.apache.flink.streaming.api.windowing.windows.GlobalWindow) ReduceSum(org.apache.flink.state.api.utils.ReduceSum) Test(org.junit.Test)

Example 2 with ReduceSum

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

the class WindowReaderTest method testReducingWindow.

@Test
public void testReducingWindow() throws Exception {
    WindowOperator<Integer, Integer, ?, Void, ?> operator = getWindowOperator(stream -> stream.window(TumblingEventTimeWindows.of(Time.milliseconds(1))).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(Arrays.asList(1, 1), 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)

Example 3 with ReduceSum

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

the class DataSetSavepointWindowReaderITCase 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);
    ExecutionEnvironment batchEnv = ExecutionEnvironment.getExecutionEnvironment();
    ExistingSavepoint savepoint = Savepoint.load(batchEnv, savepointPath, getStateBackend());
    List<Integer> results = savepoint.window(TumblingEventTimeWindows.of(Time.milliseconds(10))).reduce(uid, new ReduceSum(), Types.INT, Types.INT).collect();
    Assert.assertThat("Unexpected results from keyed state", results, Matchers.containsInAnyOrder(numbers));
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) 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 4 with ReduceSum

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

the class DataSetSavepointWindowReaderITCase 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);
    ExecutionEnvironment batchEnv = ExecutionEnvironment.getExecutionEnvironment();
    ExistingSavepoint savepoint = Savepoint.load(batchEnv, savepointPath, getStateBackend());
    List<Integer> results = savepoint.window(TumblingEventTimeWindows.of(Time.milliseconds(10))).evictor().reduce(uid, new ReduceSum(), Types.INT, Types.INT).collect();
    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) 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) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) 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) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) 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 5 with ReduceSum

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

the class DataSetSavepointWindowReaderITCase method testWindowTriggerStateReader.

@Test
public void testWindowTriggerStateReader() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStateBackend(getStateBackend());
    env.setParallelism(4);
    env.addSource(createSource(numbers)).rebalance().keyBy(id -> id).window(GlobalWindows.create()).trigger(PurgingTrigger.of(CountTrigger.of(10))).reduce(new ReduceSum()).uid(uid).addSink(new DiscardingSink<>());
    String savepointPath = takeSavepoint(env);
    ExecutionEnvironment batchEnv = ExecutionEnvironment.getExecutionEnvironment();
    ExistingSavepoint savepoint = Savepoint.load(batchEnv, savepointPath, getStateBackend());
    List<Long> results = savepoint.window(new GlobalWindow.Serializer()).reduce(uid, new ReduceSum(), new TriggerReaderFunction(), Types.INT, Types.INT, Types.LONG).collect();
    Assert.assertThat("Unexpected results from trigger state", results, Matchers.contains(1L, 1L, 1L));
}
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) 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) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) 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) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) GlobalWindow(org.apache.flink.streaming.api.windowing.windows.GlobalWindow) 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