Java Code Examples for org.apache.flink.api.common.state.MapState#remove()

The following examples show how to use org.apache.flink.api.common.state.MapState#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BroadcastState.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(Item nextItem, ReadOnlyContext ctx, Collector<String> out) throws Exception {

	final MapState<String, List<Item>> partialMatches = getRuntimeContext().getMapState(matchStateDesc);
	final Shape shapeOfNextItem = nextItem.getShape();

	System.out.println("SAW: " + nextItem);
	for (Map.Entry<String, Tuple2<Shape, Shape>> entry: ctx.getBroadcastState(broadcastStateDescriptor).immutableEntries()) {
		final String ruleName = entry.getKey();
		final Tuple2<Shape, Shape> rule = entry.getValue();

		List<Item> partialsForThisRule = partialMatches.get(ruleName);
		if (partialsForThisRule == null) {
			partialsForThisRule = new ArrayList<>();
		}

		if (shapeOfNextItem == rule.f1 && !partialsForThisRule.isEmpty()) {
			for (Item i : partialsForThisRule) {
				out.collect("MATCH: " + i + " - " + nextItem);
			}
			partialsForThisRule.clear();
		}

		if (shapeOfNextItem == rule.f0) {
			partialsForThisRule.add(nextItem);
		}

		if (partialsForThisRule.isEmpty()) {
			partialMatches.remove(ruleName);
		} else {
			partialMatches.put(ruleName, partialsForThisRule);
		}
	}
}
 
Example 2
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateIsEmpty() throws Exception {
	MapStateDescriptor<Integer, Long> kvId = new MapStateDescriptor<>("id", Integer.class, Long.class);

	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		MapState<Integer, Long> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
		backend.setCurrentKey(1);
		assertTrue(state.isEmpty());

		int stateSize = 1024;
		for (int i = 0; i < stateSize; i++) {
			state.put(i, i * 2L);
			assertFalse(state.isEmpty());
		}

		for (int i = 0; i < stateSize; i++) {
			assertFalse(state.isEmpty());
			state.remove(i);
		}
		assertTrue(state.isEmpty());

	} finally {
		backend.dispose();
	}
}