use of org.apache.flink.api.common.state.ListStateDescriptor 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));
}
use of org.apache.flink.api.common.state.ListStateDescriptor 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));
}
use of org.apache.flink.api.common.state.ListStateDescriptor in project flink by apache.
the class HeapListStateTest method testAddAndGet.
@Test
public void testAddAndGet() throws Exception {
final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);
stateDescr.initializeSerializerUnlessSet(new ExecutionConfig());
final HeapKeyedStateBackend<String> keyedBackend = createKeyedBackend();
try {
InternalListState<VoidNamespace, Long> state = keyedBackend.createListState(VoidNamespaceSerializer.INSTANCE, stateDescr);
state.setCurrentNamespace(VoidNamespace.INSTANCE);
keyedBackend.setCurrentKey("abc");
assertNull(state.get());
keyedBackend.setCurrentKey("def");
assertNull(state.get());
state.add(17L);
state.add(11L);
assertEquals(asList(17L, 11L), state.get());
keyedBackend.setCurrentKey("abc");
assertNull(state.get());
keyedBackend.setCurrentKey("g");
assertNull(state.get());
state.add(1L);
state.add(2L);
keyedBackend.setCurrentKey("def");
assertEquals(asList(17L, 11L), state.get());
state.clear();
assertNull(state.get());
keyedBackend.setCurrentKey("g");
state.add(3L);
state.add(2L);
state.add(1L);
keyedBackend.setCurrentKey("def");
assertNull(state.get());
keyedBackend.setCurrentKey("g");
assertEquals(asList(1L, 2L, 3L, 2L, 1L), state.get());
state.clear();
// make sure all lists / maps are cleared
StateTable<String, VoidNamespace, ArrayList<Long>> stateTable = ((HeapListState<String, VoidNamespace, Long>) state).stateTable;
assertTrue(stateTable.isEmpty());
} finally {
keyedBackend.close();
keyedBackend.dispose();
}
}
use of org.apache.flink.api.common.state.ListStateDescriptor in project flink by apache.
the class HeapListStateTest method testMerging.
@Test
public void testMerging() throws Exception {
final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);
stateDescr.initializeSerializerUnlessSet(new ExecutionConfig());
final Integer namespace1 = 1;
final Integer namespace2 = 2;
final Integer namespace3 = 3;
final Set<Long> expectedResult = new HashSet<>(asList(11L, 22L, 33L, 44L, 55L));
final HeapKeyedStateBackend<String> keyedBackend = createKeyedBackend();
try {
InternalListState<Integer, Long> state = keyedBackend.createListState(IntSerializer.INSTANCE, stateDescr);
// populate the different namespaces
// - abc spreads the values over three namespaces
// - def spreads teh values over two namespaces (one empty)
// - ghi is empty
// - jkl has all elements already in the target namespace
// - mno has all elements already in one source namespace
keyedBackend.setCurrentKey("abc");
state.setCurrentNamespace(namespace1);
state.add(33L);
state.add(55L);
state.setCurrentNamespace(namespace2);
state.add(22L);
state.add(11L);
state.setCurrentNamespace(namespace3);
state.add(44L);
keyedBackend.setCurrentKey("def");
state.setCurrentNamespace(namespace1);
state.add(11L);
state.add(44L);
state.setCurrentNamespace(namespace3);
state.add(22L);
state.add(55L);
state.add(33L);
keyedBackend.setCurrentKey("jkl");
state.setCurrentNamespace(namespace1);
state.add(11L);
state.add(22L);
state.add(33L);
state.add(44L);
state.add(55L);
keyedBackend.setCurrentKey("mno");
state.setCurrentNamespace(namespace3);
state.add(11L);
state.add(22L);
state.add(33L);
state.add(44L);
state.add(55L);
keyedBackend.setCurrentKey("abc");
state.mergeNamespaces(namespace1, asList(namespace2, namespace3));
state.setCurrentNamespace(namespace1);
validateResult(state.get(), expectedResult);
keyedBackend.setCurrentKey("def");
state.mergeNamespaces(namespace1, asList(namespace2, namespace3));
state.setCurrentNamespace(namespace1);
validateResult(state.get(), expectedResult);
keyedBackend.setCurrentKey("ghi");
state.mergeNamespaces(namespace1, asList(namespace2, namespace3));
state.setCurrentNamespace(namespace1);
assertNull(state.get());
keyedBackend.setCurrentKey("jkl");
state.mergeNamespaces(namespace1, asList(namespace2, namespace3));
state.setCurrentNamespace(namespace1);
validateResult(state.get(), expectedResult);
keyedBackend.setCurrentKey("mno");
state.mergeNamespaces(namespace1, asList(namespace2, namespace3));
state.setCurrentNamespace(namespace1);
validateResult(state.get(), expectedResult);
// make sure all lists / maps are cleared
keyedBackend.setCurrentKey("abc");
state.setCurrentNamespace(namespace1);
state.clear();
keyedBackend.setCurrentKey("def");
state.setCurrentNamespace(namespace1);
state.clear();
keyedBackend.setCurrentKey("ghi");
state.setCurrentNamespace(namespace1);
state.clear();
keyedBackend.setCurrentKey("jkl");
state.setCurrentNamespace(namespace1);
state.clear();
keyedBackend.setCurrentKey("mno");
state.setCurrentNamespace(namespace1);
state.clear();
StateTable<String, Integer, ArrayList<Long>> stateTable = ((HeapListState<String, Integer, Long>) state).stateTable;
assertTrue(stateTable.isEmpty());
} finally {
keyedBackend.close();
keyedBackend.dispose();
}
}
use of org.apache.flink.api.common.state.ListStateDescriptor in project flink by apache.
the class StateBackendTestBase method testEmptyStateCheckpointing.
@Test
public void testEmptyStateCheckpointing() {
try {
CheckpointStreamFactory streamFactory = createStreamFactory();
AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
ListStateDescriptor<String> kvId = new ListStateDescriptor<>("id", String.class);
// draw a snapshot
KeyGroupsStateHandle snapshot = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462379L, 1, streamFactory, CheckpointOptions.forFullCheckpoint()));
assertNull(snapshot);
backend.dispose();
backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot);
backend.dispose();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations