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

The following examples show how to use org.apache.flink.api.common.state.ReducingState#get() . 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: 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 2
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 3
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 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: 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 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: 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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReducingStateRestoreWithWrongSerializers() throws Exception {
	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ReducingStateDescriptor<String> kvId = new ReducingStateDescriptor<>("id",
				new AppendingReduce(),
				StringSerializer.INSTANCE);
		ReducingState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.add("1");
		backend.setCurrentKey(2);
		state.add("2");

		// draw a snapshot
		KeyedStateHandle snapshot1 = runSnapshot(
			backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);

		backend.dispose();
		// restore the first snapshot and validate it
		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
		snapshot1.discardState();

		@SuppressWarnings("unchecked")
		TypeSerializer<String> fakeStringSerializer =
				(TypeSerializer<String>) (TypeSerializer<?>) FloatSerializer.INSTANCE;

		try {
			kvId = new ReducingStateDescriptor<>("id", new AppendingReduce(), fakeStringSerializer);

			state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

			state.get();

			fail("should recognize wrong serializers");
		} catch (StateMigrationException ignored) {
			// expected
		}
	} finally {
		backend.dispose();
	}
}
 
Example 18
Source File: TtlReducingStateVerifier.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
Integer getInternal(@Nonnull ReducingState<Integer> state) throws Exception {
	return state.get();
}
 
Example 19
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReducingStateRestoreWithWrongSerializers() throws Exception {
	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ReducingStateDescriptor<String> kvId = new ReducingStateDescriptor<>("id",
				new AppendingReduce(),
				StringSerializer.INSTANCE);
		ReducingState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.add("1");
		backend.setCurrentKey(2);
		state.add("2");

		// draw a snapshot
		KeyedStateHandle snapshot1 = runSnapshot(
			backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);

		backend.dispose();
		// restore the first snapshot and validate it
		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
		snapshot1.discardState();

		@SuppressWarnings("unchecked")
		TypeSerializer<String> fakeStringSerializer =
				(TypeSerializer<String>) (TypeSerializer<?>) FloatSerializer.INSTANCE;

		try {
			kvId = new ReducingStateDescriptor<>("id", new AppendingReduce(), fakeStringSerializer);

			state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

			state.get();

			fail("should recognize wrong serializers");
		} catch (StateMigrationException ignored) {
			// expected
		}
	} finally {
		backend.dispose();
	}
}
 
Example 20
Source File: TtlReducingStateVerifier.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
Integer getInternal(@Nonnull ReducingState<Integer> state) throws Exception {
	return state.get();
}