org.apache.flink.api.common.state.ReducingState Java Examples

The following examples show how to use org.apache.flink.api.common.state.ReducingState. 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 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 with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {

	if (window.maxTimestamp() <= ctx.getCurrentWatermark()) {
		// if the watermark is already past the window fire immediately
		return TriggerResult.FIRE;
	} else {
		ctx.registerEventTimeTimer(window.maxTimestamp());
	}

	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;
		ctx.registerEventTimeTimer(nextFireTimestamp);
		fireTimestamp.add(nextFireTimestamp);
	}

	return TriggerResult.CONTINUE;
}
 
Example #3
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 #4
Source File: ContinuousEventTimeTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {

	if (window.maxTimestamp() <= ctx.getCurrentWatermark()) {
		// if the watermark is already past the window fire immediately
		return TriggerResult.FIRE;
	} else {
		ctx.registerEventTimeTimer(window.maxTimestamp());
	}

	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;
		ctx.registerEventTimeTimer(nextFireTimestamp);
		fireTimestamp.add(nextFireTimestamp);
	}

	return TriggerResult.CONTINUE;
}
 
Example #5
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 #6
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 #7
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);

	timestamp = ctx.getCurrentProcessingTime();

	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;

		ctx.registerProcessingTimeTimer(nextFireTimestamp);

		fireTimestamp.add(nextFireTimestamp);
		return TriggerResult.CONTINUE;
	}
	return TriggerResult.CONTINUE;
}
 
Example #8
Source File: ContinuousProcessingTimeTrigger.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);

	timestamp = ctx.getCurrentProcessingTime();

	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;

		ctx.registerProcessingTimeTimer(nextFireTimestamp);

		fireTimestamp.add(nextFireTimestamp);
		return TriggerResult.CONTINUE;
	}
	return TriggerResult.CONTINUE;
}
 
Example #9
Source File: ContinuousProcessingTimeTrigger.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {
	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);

	timestamp = ctx.getCurrentProcessingTime();

	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;

		ctx.registerProcessingTimeTimer(nextFireTimestamp);

		fireTimestamp.add(nextFireTimestamp);
		return TriggerResult.CONTINUE;
	}
	return TriggerResult.CONTINUE;
}
 
Example #10
Source File: ContinuousEventTimeTrigger.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx) throws Exception {

	if (window.maxTimestamp() <= ctx.getCurrentWatermark()) {
		// if the watermark is already past the window fire immediately
		return TriggerResult.FIRE;
	} else {
		ctx.registerEventTimeTimer(window.maxTimestamp());
	}

	ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
	if (fireTimestamp.get() == null) {
		long start = timestamp - (timestamp % interval);
		long nextFireTimestamp = start + interval;
		ctx.registerEventTimeTimer(nextFireTimestamp);
		fireTimestamp.add(nextFireTimestamp);
	}

	return TriggerResult.CONTINUE;
}
 
Example #11
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 #12
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 #13
Source File: SavepointRuntimeContext.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
	if (!stateRegistrationAllowed) {
		throw new RuntimeException(REGISTRATION_EXCEPTION_MSG);
	}

	registeredDescriptors.add(stateProperties);
	return keyedStateStore.getReducingState(stateProperties);
}
 
Example #14
Source File: DefaultKeyedStateStore.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		return getPartitionedState(stateProperties);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example #15
Source File: DefaultKeyedStateStore.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		return getPartitionedState(stateProperties);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example #16
Source File: DefaultKeyedStateStore.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		return getPartitionedState(stateProperties);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: SavepointRuntimeContext.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
	if (!stateRegistrationAllowed) {
		throw new RuntimeException(REGISTRATION_EXCEPTION_MSG);
	}

	registeredDescriptors.add(stateProperties);
	return keyedStateStore.getReducingState(stateProperties);
}
 
Example #23
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 #24
Source File: CountTrigger.java    From flink 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 #25
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 #26
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 #27
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 #28
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 #29
Source File: ProcessingTimeTriggers.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> nextFiring = ctx.getPartitionedState(nextFiringStateDesc);
	if (nextFiring.get() == null) {
		long nextTimer = ctx.getCurrentProcessingTime() + interval;
		ctx.registerProcessingTimeTimer(nextTimer);
		nextFiring.add(nextTimer);
	}
	return false;
}
 
Example #30
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;
	}
}