org.apache.flink.streaming.api.TimerService Java Examples

The following examples show how to use org.apache.flink.streaming.api.TimerService. 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: EventTimeJoinExercise.java    From flink-training-exercises with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement1(Trade trade,
							Context context,
							Collector<EnrichedTrade> out)
		throws Exception {

	System.out.println("Received " + trade.toString());
	TimerService timerService = context.timerService();

	if (context.timestamp() > timerService.currentWatermark()) {
		// Do the join later, by which time any relevant Customer records should have have arrived.
		tradeMap.put(trade.timestamp, trade);
		timerService.registerEventTimeTimer(trade.timestamp);
	} else {
		// Late Trades land here.
	}
}
 
Example #2
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	collector = new TimestampedCollector<>(output);

	this.broadcastStates = new HashMap<>(broadcastStateDescriptors.size());
	for (MapStateDescriptor<?, ?> descriptor: broadcastStateDescriptors) {
		broadcastStates.put(descriptor, getOperatorStateBackend().getBroadcastState(descriptor));
	}

	rwContext = new ReadWriteContextImpl(getExecutionConfig(), getKeyedStateBackend(), userFunction, broadcastStates, timerService);
	rContext = new ReadOnlyContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
	onTimerContext = new OnTimerContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
}
 
Example #3
Source File: KeyedProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	final TimerService timerService = ctx.timerService();
	final ValueState<Integer> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT:" + value);
		state.update(value);
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
		} else {
			timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
		}
	} else {
		state.clear();
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
		} else {
			timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
		}
	}
}
 
Example #4
Source File: CleanupState.java    From flink with Apache License 2.0 6 votes vote down vote up
default void registerProcessingCleanupTimer(
		ValueState<Long> cleanupTimeState,
		long currentTime,
		long minRetentionTime,
		long maxRetentionTime,
		TimerService timerService) throws Exception {

	// last registered timer
	Long curCleanupTime = cleanupTimeState.value();

	// check if a cleanup timer is registered and
	// that the current cleanup timer won't delete state we need to keep
	if (curCleanupTime == null || (currentTime + minRetentionTime) > curCleanupTime) {
		// we need to register a new (later) timer
		long cleanupTime = currentTime + maxRetentionTime;
		// register timer and remember clean-up time
		timerService.registerProcessingTimeTimer(cleanupTime);
		// delete expired timer
		if (curCleanupTime != null) {
			timerService.deleteProcessingTimeTimer(curCleanupTime);
		}
		cleanupTimeState.update(cleanupTime);
	}
}
 
Example #5
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	collector = new TimestampedCollector<>(output);

	this.broadcastStates = new HashMap<>(broadcastStateDescriptors.size());
	for (MapStateDescriptor<?, ?> descriptor: broadcastStateDescriptors) {
		broadcastStates.put(descriptor, getOperatorStateBackend().getBroadcastState(descriptor));
	}

	rwContext = new ReadWriteContextImpl(getExecutionConfig(), getKeyedStateBackend(), userFunction, broadcastStates, timerService);
	rContext = new ReadOnlyContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
	onTimerContext = new OnTimerContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
}
 
Example #6
Source File: KeyedProcessOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	final TimerService timerService = ctx.timerService();
	final ValueState<Integer> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT:" + value);
		state.update(value);
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
		} else {
			timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
		}
	} else {
		state.clear();
		if (expectedTimeDomain.equals(TimeDomain.EVENT_TIME)) {
			timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
		} else {
			timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
		}
	}
}
 
Example #7
Source File: CoBroadcastWithKeyedOperator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	collector = new TimestampedCollector<>(output);

	this.broadcastStates = new HashMap<>(broadcastStateDescriptors.size());
	for (MapStateDescriptor<?, ?> descriptor: broadcastStateDescriptors) {
		broadcastStates.put(descriptor, getOperatorStateBackend().getBroadcastState(descriptor));
	}

	rwContext = new ReadWriteContextImpl(getExecutionConfig(), getKeyedStateBackend(), userFunction, broadcastStates, timerService);
	rContext = new ReadOnlyContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
	onTimerContext = new OnTimerContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
}
 
Example #8
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example #9
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example #10
Source File: CoBroadcastWithKeyedOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
ReadOnlyContextImpl(
		final ExecutionConfig executionConfig,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #11
Source File: KeyedStateBootstrapOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	Supplier<InternalTimerService<VoidNamespace>> internalTimerService = () -> getInternalTimerService(
		"user-timers",
		VoidNamespaceSerializer.INSTANCE,
		VoidTriggerable.instance());

	TimerService timerService = new LazyTimerService(internalTimerService, getProcessingTimeService());

	context = new KeyedStateBootstrapOperator<K, IN>.ContextImpl(userFunction, timerService);
}
 
Example #12
Source File: KeyedCoProcessOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();
	collector = new TimestampedCollector<>(output);

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new ContextImpl<>(userFunction, timerService);
	onTimerContext = new OnTimerContextImpl<>(userFunction, timerService);
}
 
Example #13
Source File: CoBroadcastWithKeyedOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
ReadWriteContextImpl (
		final ExecutionConfig executionConfig,
		final KeyedStateBackend<KS> keyedStateBackend,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.keyedStateBackend = Preconditions.checkNotNull(keyedStateBackend);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #14
Source File: KeyedCoProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example #15
Source File: KeyedProcessOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();
	collector = new TimestampedCollector<>(output);

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new ContextImpl(userFunction, timerService);
	onTimerContext = new OnTimerContextImpl(userFunction, timerService);
}
 
Example #16
Source File: LongRidesSolution.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
	TimerService timerService = context.timerService();

	if (ride.isStart) {
		// the matching END might have arrived first; don't overwrite it
		if (rideState.value() == null) {
			rideState.update(ride);
		}
	} else {
		rideState.update(ride);
	}

	timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
 
Example #17
Source File: KeyedStateBootstrapOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	InternalTimerService<VoidNamespace> internalTimerService = getInternalTimerService(
		"user-timers",
		VoidNamespaceSerializer.INSTANCE,
		VoidTriggerable.instance());

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new KeyedStateBootstrapOperator<K, IN>.ContextImpl(userFunction, timerService);
}
 
Example #18
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
ReadWriteContextImpl (
		final ExecutionConfig executionConfig,
		final KeyedStateBackend<KS> keyedStateBackend,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.keyedStateBackend = Preconditions.checkNotNull(keyedStateBackend);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #19
Source File: CheckpointedLongRidesSolution.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
	TimerService timerService = context.timerService();

	if (ride.isStart) {
		// the matching END might have arrived first (out of order); don't overwrite it
		if (rideState.value() == null) {
			rideState.update(ride);
		}
	} else {
		rideState.update(ride);
	}

	timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
 
Example #20
Source File: KeyedCoProcessOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();
	collector = new TimestampedCollector<>(output);

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new ContextImpl<>(userFunction, timerService);
	onTimerContext = new OnTimerContextImpl<>(userFunction, timerService);
}
 
Example #21
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
ReadOnlyContextImpl(
		final ExecutionConfig executionConfig,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #22
Source File: KeyedProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	final TimerService timerService = ctx.timerService();

	timerService.registerProcessingTimeTimer(3);
	timerService.registerEventTimeTimer(4);
	timerService.registerProcessingTimeTimer(5);
	timerService.registerEventTimeTimer(6);
	timerService.deleteProcessingTimeTimer(3);
	timerService.deleteEventTimeTimer(4);
}
 
Example #23
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
ReadWriteContextImpl (
		final ExecutionConfig executionConfig,
		final KeyedStateBackend<KS> keyedStateBackend,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.keyedStateBackend = Preconditions.checkNotNull(keyedStateBackend);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #24
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
ReadOnlyContextImpl(
		final ExecutionConfig executionConfig,
		final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function,
		final Map<MapStateDescriptor<?, ?>, BroadcastState<?, ?>> broadcastStates,
		final TimerService timerService) {

	function.super();
	this.config = Preconditions.checkNotNull(executionConfig);
	this.states = Preconditions.checkNotNull(broadcastStates);
	this.timerService = Preconditions.checkNotNull(timerService);
}
 
Example #25
Source File: KeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 5);
	} else {
		state.clear();
		timerService.deleteProcessingTimeTimer(timerService.currentProcessingTime() + 4);
	}
}
 
Example #26
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example #27
Source File: LegacyKeyedProcessOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();
	collector = new TimestampedCollector<>(output);

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new ContextImpl(userFunction, timerService);
	onTimerContext = new OnTimerContextImpl(userFunction, timerService);
}
 
Example #28
Source File: LegacyKeyedCoProcessOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleValue(
	Object value,
	Collector<String> out,
	TimerService timerService,
	int channel) throws IOException {
	final ValueState<String> state = getRuntimeContext().getState(this.state);
	if (state.value() == null) {
		out.collect("INPUT" + channel + ":" + value);
		state.update(String.valueOf(value));
		timerService.registerEventTimeTimer(timerService.currentWatermark() + 5);
	} else {
		state.clear();
		timerService.deleteEventTimeTimer(timerService.currentWatermark() + 4);
	}
}
 
Example #29
Source File: LegacyKeyedProcessOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();
	collector = new TimestampedCollector<>(output);

	InternalTimerService<VoidNamespace> internalTimerService =
			getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);

	TimerService timerService = new SimpleTimerService(internalTimerService);

	context = new ContextImpl(userFunction, timerService);
	onTimerContext = new OnTimerContextImpl(userFunction, timerService);
}
 
Example #30
Source File: KeyedCoProcessOperator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public TimerService timerService() {
	return timerService;
}