org.apache.flink.runtime.state.KeyedStateFunction Java Examples

The following examples show how to use org.apache.flink.runtime.state.KeyedStateFunction. 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: HeapKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public <N, S extends State, T> void applyToAllKeys(
	final N namespace,
	final TypeSerializer<N> namespaceSerializer,
	final StateDescriptor<S, T> stateDescriptor,
	final KeyedStateFunction<K, S> function) throws Exception {

	try (Stream<K> keyStream = getKeys(stateDescriptor.getName(), namespace)) {

		// we copy the keys into list to avoid the concurrency problem
		// when state.clear() is invoked in function.process().
		final List<K> keys = keyStream.collect(Collectors.toList());

		final S state = getPartitionedState(
			namespace,
			namespaceSerializer,
			stateDescriptor);

		for (K key : keys) {
			setCurrentKey(key);
			function.process(key, state);
		}
	}
}
 
Example #2
Source File: CoBroadcastWithKeyedOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	// put an element in the broadcast state
	ctx.applyToKeyedState(
			listStateDesc,
			new KeyedStateFunction<String, ListState<String>>() {
				@Override
				public void process(String key, ListState<String> state) throws Exception {
					final Iterator<String> it = state.get().iterator();

					final List<String> list = new ArrayList<>();
					while (it.hasNext()) {
						list.add(it.next());
					}
					assertEquals(expectedKeyedStates.get(key), list);
				}
			});
}
 
Example #3
Source File: HeapKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, S extends State, T> void applyToAllKeys(
	final N namespace,
	final TypeSerializer<N> namespaceSerializer,
	final StateDescriptor<S, T> stateDescriptor,
	final KeyedStateFunction<K, S> function) throws Exception {

	try (Stream<K> keyStream = getKeys(stateDescriptor.getName(), namespace)) {

		// we copy the keys into list to avoid the concurrency problem
		// when state.clear() is invoked in function.process().
		final List<K> keys = keyStream.collect(Collectors.toList());

		final S state = getPartitionedState(
			namespace,
			namespaceSerializer,
			stateDescriptor);

		for (K key : keys) {
			setCurrentKey(key);
			function.process(key, state);
		}
	}
}
 
Example #4
Source File: CepOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void migrateOldState() throws Exception {
	getKeyedStateBackend().applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		new ValueStateDescriptor<>(
			"nfaOperatorStateName",
			new NFA.NFASerializer<>(inputSerializer)
		),
		new KeyedStateFunction<Object, ValueState<MigratedNFA<IN>>>() {
			@Override
			public void process(Object key, ValueState<MigratedNFA<IN>> state) throws Exception {
				MigratedNFA<IN> oldState = state.value();
				computationStates.update(new NFAState(oldState.getComputationStates()));
				org.apache.flink.cep.nfa.SharedBuffer<IN> sharedBuffer = oldState.getSharedBuffer();
				partialMatches.init(sharedBuffer.getEventsBuffer(), sharedBuffer.getPages());
				state.clear();
			}
		}
	);
}
 
Example #5
Source File: OngoingRidesExercise.java    From flink-training-exercises with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(String msg, Context ctx, Collector<TaxiRide> out) throws Exception {
	DateTimeFormatter timeFormatter =
			DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.US).withZoneUTC();

	Long thresholdInMinutes = Long.valueOf(msg);
	Long wm = ctx.currentWatermark();
	System.out.println("QUERY: " + thresholdInMinutes + " minutes at " + timeFormatter.print(wm));

	// Collect to the output all ongoing rides that started at least thresholdInMinutes ago.
	ctx.applyToKeyedState(taxiDescriptor, new KeyedStateFunction<Long, ValueState<TaxiRide>>() {
		@Override
		public void process(Long taxiId, ValueState<TaxiRide> taxiState) throws Exception {
			throw new MissingSolutionException();
		}
	});
}
 
Example #6
Source File: OngoingRidesSolution.java    From flink-training-exercises with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(String msg, Context ctx, Collector<TaxiRide> out) throws Exception {
	DateTimeFormatter timeFormatter =
			DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.US).withZoneUTC();

	Long thresholdInMinutes = Long.valueOf(msg);
	Long wm = ctx.currentWatermark();
	System.out.println("QUERY: " + thresholdInMinutes + " minutes at " + timeFormatter.print(wm));

	// Collect to the output all ongoing rides that started at least thresholdInMinutes ago.
	ctx.applyToKeyedState(taxiDescriptor, new KeyedStateFunction<Long, ValueState<TaxiRide>>() {
		@Override
		public void process(Long taxiId, ValueState<TaxiRide> taxiState) throws Exception {
			TaxiRide ride = taxiState.value();
			if (ride.isStart) {
				long minutes = (wm - ride.getEventTime()) / 60000;
				if (ride.isStart && (minutes >= thresholdInMinutes)) {
					out.collect(ride);
				}
			}
		}
	});
}
 
Example #7
Source File: TaxiQuerySolution.java    From flink-training-exercises with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(String query,
									Context ctx,
									Collector<Tuple2<String, String>> out) throws Exception {

	out.collect(new Tuple2<>("QUERY", query));

	ExpressionEvaluator ee = cookBooleanExpression(query);
	ctx.getBroadcastState(queryDescriptor).put(QUERY_KEY, ee);

	ctx.applyToKeyedState(rideDescriptor, new KeyedStateFunction<Long, ValueState<TaxiRide>>() {
		@Override
		public void process(Long taxiId, ValueState<TaxiRide> taxiState) throws Exception {
			TaxiRide ride = taxiState.value();

			if (evaluateBooleanExpression(ee, ride, ctx.currentWatermark())) {
				out.collect(new Tuple2<>("PBE@" + timeFormatter.print(ctx.currentWatermark()), ride.toString()));
			}
		}
	});
}
 
Example #8
Source File: CoBroadcastWithKeyedOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	// put an element in the broadcast state
	ctx.applyToKeyedState(
			listStateDesc,
			new KeyedStateFunction<String, ListState<String>>() {
				@Override
				public void process(String key, ListState<String> state) throws Exception {
					final Iterator<String> it = state.get().iterator();

					final List<String> list = new ArrayList<>();
					while (it.hasNext()) {
						list.add(it.next());
					}
					assertEquals(expectedKeyedStates.get(key), list);
				}
			});
}
 
Example #9
Source File: HeapKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, S extends State, T> void applyToAllKeys(
	final N namespace,
	final TypeSerializer<N> namespaceSerializer,
	final StateDescriptor<S, T> stateDescriptor,
	final KeyedStateFunction<K, S> function) throws Exception {

	try (Stream<K> keyStream = getKeys(stateDescriptor.getName(), namespace)) {

		// we copy the keys into list to avoid the concurrency problem
		// when state.clear() is invoked in function.process().
		final List<K> keys = keyStream.collect(Collectors.toList());

		final S state = getPartitionedState(
			namespace,
			namespaceSerializer,
			stateDescriptor);

		for (K key : keys) {
			setCurrentKey(key);
			function.process(key, state);
		}
	}
}
 
Example #10
Source File: CepOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void migrateOldState() throws Exception {
	getKeyedStateBackend().applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		new ValueStateDescriptor<>(
			"nfaOperatorStateName",
			new NFA.NFASerializer<>(inputSerializer)
		),
		new KeyedStateFunction<Object, ValueState<MigratedNFA<IN>>>() {
			@Override
			public void process(Object key, ValueState<MigratedNFA<IN>> state) throws Exception {
				MigratedNFA<IN> oldState = state.value();
				computationStates.update(new NFAState(oldState.getComputationStates()));
				org.apache.flink.cep.nfa.SharedBuffer<IN> sharedBuffer = oldState.getSharedBuffer();
				partialMatches.init(sharedBuffer.getEventsBuffer(), sharedBuffer.getPages());
				state.clear();
			}
		}
	);
}
 
Example #11
Source File: CoBroadcastWithKeyedOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(Integer value, Context ctx, Collector<String> out) throws Exception {
	// put an element in the broadcast state
	ctx.applyToKeyedState(
			listStateDesc,
			new KeyedStateFunction<String, ListState<String>>() {
				@Override
				public void process(String key, ListState<String> state) throws Exception {
					final Iterator<String> it = state.get().iterator();

					final List<String> list = new ArrayList<>();
					while (it.hasNext()) {
						list.add(it.next());
					}
					assertEquals(expectedKeyedStates.get(key), list);
				}
			});
}
 
Example #12
Source File: CepOperator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void migrateOldState() throws Exception {
	getKeyedStateBackend().applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		new ValueStateDescriptor<>(
			"nfaOperatorStateName",
			new NFA.NFASerializer<>(inputSerializer)
		),
		new KeyedStateFunction<Object, ValueState<MigratedNFA<IN>>>() {
			@Override
			public void process(Object key, ValueState<MigratedNFA<IN>> state) throws Exception {
				MigratedNFA<IN> oldState = state.value();
				computationStates.update(new NFAState(oldState.getComputationStates()));
				org.apache.flink.cep.nfa.SharedBuffer<IN> sharedBuffer = oldState.getSharedBuffer();
				partialMatches.init(sharedBuffer.getEventsBuffer(), sharedBuffer.getPages());
				state.clear();
			}
		}
	);
}
 
Example #13
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception {

	keyedStateBackend.applyToAllKeys(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE,
			Preconditions.checkNotNull(stateDescriptor),
			Preconditions.checkNotNull(function));
}
 
Example #14
Source File: StateBackendBenchmarkUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <K, S extends State, T> void applyToAllKeys(
	KeyedStateBackend<K> backend,
	final StateDescriptor<S, T> stateDescriptor,
	final KeyedStateFunction<K, S> function) throws Exception {
	backend.applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		stateDescriptor,
		function);
}
 
Example #15
Source File: CoBroadcastWithKeyedOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception {

	keyedStateBackend.applyToAllKeys(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE,
			Preconditions.checkNotNull(stateDescriptor),
			Preconditions.checkNotNull(function));
}
 
Example #16
Source File: CoBroadcastWithKeyedOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception {

	keyedStateBackend.applyToAllKeys(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE,
			Preconditions.checkNotNull(stateDescriptor),
			Preconditions.checkNotNull(function));
}
 
Example #17
Source File: ReductionsTest.java    From stateful-functions with Apache License 2.0 4 votes vote down vote up
@Override
public <N, S extends State, T> void applyToAllKeys(
    N namespace,
    TypeSerializer<N> namespaceSerializer,
    StateDescriptor<S, T> stateDescriptor,
    KeyedStateFunction<Object, S> function) {}
 
Example #18
Source File: ReductionsTest.java    From flink-statefun with Apache License 2.0 4 votes vote down vote up
@Override
public <N, S extends State, T> void applyToAllKeys(
    N namespace,
    TypeSerializer<N> namespaceSerializer,
    StateDescriptor<S, T> stateDescriptor,
    KeyedStateFunction<Object, S> function) {}
 
Example #19
Source File: KeyedBroadcastProcessFunction.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the provided {@code function} to the state
 * associated with the provided {@code state descriptor}.
 *
 * @param stateDescriptor the descriptor of the state to be processed.
 * @param function the function to be applied.
 */
public abstract <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception;
 
Example #20
Source File: KeyedBroadcastProcessFunction.java    From Flink-CEPplus with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the provided {@code function} to the state
 * associated with the provided {@code state descriptor}.
 *
 * @param stateDescriptor the descriptor of the state to be processed.
 * @param function the function to be applied.
 */
public abstract <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception;
 
Example #21
Source File: KeyedBroadcastProcessFunction.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Applies the provided {@code function} to the state
 * associated with the provided {@code state descriptor}.
 *
 * @param stateDescriptor the descriptor of the state to be processed.
 * @param function the function to be applied.
 */
public abstract <VS, S extends State> void applyToKeyedState(
		final StateDescriptor<S, VS> stateDescriptor,
		final KeyedStateFunction<KS, S> function) throws Exception;