Java Code Examples for org.apache.flink.api.common.state.ReducingState#clear()

The following examples show how to use org.apache.flink.api.common.state.ReducingState#clear() . 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: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that an empty {@code ReduceState} yields {@code null}.
 */
@Test
public void testReducingStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ReducingStateDescriptor<String> kvId = new ReducingStateDescriptor<>("id", new AppendingReduce(), String.class);

	ReducingState<String> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertNull(state.get());

	state.add("Ciao");
	assertEquals("Ciao", state.get());

	state.clear();
	assertNull(state.get());

	backend.dispose();
}
 
Example 2
Source File: ContinuousEventTimeTrigger.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception {

	if (time == window.maxTimestamp()){
		return TriggerResult.FIRE;
	}

	ReducingState<Long> fireTimestampState = ctx.getPartitionedState(stateDesc);

	Long fireTimestamp = fireTimestampState.get();

	if (fireTimestamp != null && fireTimestamp == time) {
		fireTimestampState.clear();
		fireTimestampState.add(time + interval);
		ctx.registerEventTimeTimer(time + interval);
		return TriggerResult.FIRE;
	}

	return TriggerResult.CONTINUE;
}
 
Example 3
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that an empty {@code ReduceState} yields {@code null}.
 */
@Test
public void testReducingStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	ReducingStateDescriptor<String> kvId = new ReducingStateDescriptor<>("id", new AppendingReduce(), String.class);

	ReducingState<String> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertNull(state.get());

	state.add("Ciao");
	assertEquals("Ciao", state.get());

	state.clear();
	assertNull(state.get());

	backend.dispose();
}
 
Example 4
Source File: ContinuousEventTimeTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception {

	if (time == window.maxTimestamp()){
		return TriggerResult.FIRE;
	}

	ReducingState<Long> fireTimestampState = ctx.getPartitionedState(stateDesc);

	Long fireTimestamp = fireTimestampState.get();

	if (fireTimestamp != null && fireTimestamp == time) {
		fireTimestampState.clear();
		fireTimestampState.add(time + interval);
		ctx.registerEventTimeTimer(time + interval);
		return TriggerResult.FIRE;
	}

	return TriggerResult.CONTINUE;
}
 
Example 5
Source File: ContinuousEventTimeTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception {

	if (time == window.maxTimestamp()){
		return TriggerResult.FIRE;
	}

	ReducingState<Long> fireTimestampState = ctx.getPartitionedState(stateDesc);

	Long fireTimestamp = fireTimestampState.get();

	if (fireTimestamp != null && fireTimestamp == time) {
		fireTimestampState.clear();
		fireTimestampState.add(time + interval);
		ctx.registerEventTimeTimer(time + interval);
		return TriggerResult.FIRE;
	}

	return TriggerResult.CONTINUE;
}
 
Example 6
Source File: ProcessingTimeTriggers.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window) throws Exception {
	ReducingState<Long> nextFiring = ctx.getPartitionedState(nextFiringStateDesc);
	Long timer = nextFiring.get();
	if (timer != null) {
		ctx.deleteProcessingTimeTimer(timer);
		nextFiring.clear();
	}
}
 
Example 7
Source File: ProcessingTimeTriggers.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onProcessingTime(long time, W window) throws Exception {
	ReducingState<Long> nextFiring = ctx.getPartitionedState(nextFiringStateDesc);
	Long timer = nextFiring.get();
	if (timer != null && timer == time) {
		long newTimer = time + interval;
		ctx.registerProcessingTimeTimer(newTimer);
		nextFiring.clear();
		nextFiring.add(newTimer);
		return true;
	} else {
		return false;
	}
}
 
Example 8
Source File: ElementTriggers.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onElement(Object element, long timestamp, W window) throws Exception {
	ReducingState<Long> count = ctx.getPartitionedState(countStateDesc);
	count.add(1L);
	if (count.get() >= countElems) {
		count.clear();
		return true;
	} else {
		return false;
	}
}
 
Example 9
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	long timestamp = fireTimestamp.get();
	ctx.deleteProcessingTimeTimer(timestamp);
	fireTimestamp.clear();
}
 
Example 10
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TriggerResult onProcessingTime(long time, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);

	if (fireTimestamp.get().equals(time)) {
		fireTimestamp.clear();
		fireTimestamp.add(time + interval);
		ctx.registerProcessingTimeTimer(time + interval);
		return TriggerResult.FIRE;
	}
	return TriggerResult.CONTINUE;
}
 
Example 11
Source File: ContinuousEventTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	Long timestamp = fireTimestamp.get();
	if (timestamp != null) {
		ctx.deleteEventTimeTimer(timestamp);
		fireTimestamp.clear();
	}
}
 
Example 12
Source File: ContinuousEventTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	Long timestamp = fireTimestamp.get();
	if (timestamp != null) {
		ctx.deleteEventTimeTimer(timestamp);
		fireTimestamp.clear();
	}
}
 
Example 13
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	long timestamp = fireTimestamp.get();
	ctx.deleteProcessingTimeTimer(timestamp);
	fireTimestamp.clear();
}
 
Example 14
Source File: ProcessingTimeTriggers.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onProcessingTime(long time, W window) throws Exception {
	ReducingState<Long> nextFiring = ctx.getPartitionedState(nextFiringStateDesc);
	Long timer = nextFiring.get();
	if (timer != null && timer == time) {
		long newTimer = time + interval;
		ctx.registerProcessingTimeTimer(newTimer);
		nextFiring.clear();
		nextFiring.add(newTimer);
		return true;
	} else {
		return false;
	}
}
 
Example 15
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TriggerResult onProcessingTime(long time, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);

	if (fireTimestamp.get().equals(time)) {
		fireTimestamp.clear();
		fireTimestamp.add(time + interval);
		ctx.registerProcessingTimeTimer(time + interval);
		return TriggerResult.FIRE;
	}
	return TriggerResult.CONTINUE;
}
 
Example 16
Source File: ContinuousProcessingTimeTrigger.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	long timestamp = fireTimestamp.get();
	ctx.deleteProcessingTimeTimer(timestamp);
	fireTimestamp.clear();
}
 
Example 17
Source File: CountTrigger.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> count = ctx.getPartitionedState(stateDesc);
	count.add(1L);
	if (count.get() >= maxCount) {
		count.clear();
		return TriggerResult.FIRE;
	}
	return TriggerResult.CONTINUE;
}
 
Example 18
Source File: ContinuousEventTimeTrigger.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	Long timestamp = fireTimestamp.get();
	if (timestamp != null) {
		ctx.deleteEventTimeTimer(timestamp);
		fireTimestamp.clear();
	}
}
 
Example 19
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testReducingStateAddAndGet() throws Exception {

	final ReducingStateDescriptor<Long> stateDescr =
		new ReducingStateDescriptor<>("my-state", (a, b) -> a + b, Long.class);

	AbstractKeyedStateBackend<String> keyedBackend = createKeyedBackend(StringSerializer.INSTANCE);

	try {
		ReducingState<Long> state =
			keyedBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, stateDescr);

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		keyedBackend.setCurrentKey("def");
		assertNull(state.get());
		state.add(17L);
		state.add(11L);
		assertEquals(28L, state.get().longValue());

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		keyedBackend.setCurrentKey("g");
		assertNull(state.get());
		state.add(1L);
		state.add(2L);

		keyedBackend.setCurrentKey("def");
		assertEquals(28L, state.get().longValue());
		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(9L, state.get().longValue());
		state.clear();

		// make sure all lists / maps are cleared
		assertThat("State backend is not empty.", keyedBackend.numKeyValueStateEntries(), is(0));
	}
	finally {
		keyedBackend.close();
		keyedBackend.dispose();
	}
}
 
Example 20
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testReducingStateAddAndGet() throws Exception {

	final ReducingStateDescriptor<Long> stateDescr =
		new ReducingStateDescriptor<>("my-state", (a, b) -> a + b, Long.class);

	AbstractKeyedStateBackend<String> keyedBackend = createKeyedBackend(StringSerializer.INSTANCE);

	try {
		ReducingState<Long> state =
			keyedBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, stateDescr);

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		keyedBackend.setCurrentKey("def");
		assertNull(state.get());
		state.add(17L);
		state.add(11L);
		assertEquals(28L, state.get().longValue());

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		keyedBackend.setCurrentKey("g");
		assertNull(state.get());
		state.add(1L);
		state.add(2L);

		keyedBackend.setCurrentKey("def");
		assertEquals(28L, state.get().longValue());
		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(9L, state.get().longValue());
		state.clear();

		// make sure all lists / maps are cleared
		assertThat("State backend is not empty.", keyedBackend.numKeyValueStateEntries(), is(0));
	}
	finally {
		keyedBackend.close();
		keyedBackend.dispose();
	}
}