Java Code Examples for org.apache.flink.api.common.state.StateDescriptor#initializeSerializerUnlessSet()

The following examples show how to use org.apache.flink.api.common.state.StateDescriptor#initializeSerializerUnlessSet() . 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: AbstractKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * @see KeyedStateBackend
 */
@Override
@SuppressWarnings("unchecked")
public <N, S extends State, V> S getOrCreateKeyedState(
		final TypeSerializer<N> namespaceSerializer,
		StateDescriptor<S, V> stateDescriptor) throws Exception {
	checkNotNull(namespaceSerializer, "Namespace serializer");
	checkNotNull(keySerializerProvider, "State key serializer has not been configured in the config. " +
			"This operation cannot use partitioned state.");

	InternalKvState<K, ?, ?> kvState = keyValueStatesByName.get(stateDescriptor.getName());
	if (kvState == null) {
		if (!stateDescriptor.isSerializerInitialized()) {
			stateDescriptor.initializeSerializerUnlessSet(executionConfig);
		}
		kvState = TtlStateFactory.createStateAndWrapWithTtlIfEnabled(
			namespaceSerializer, stateDescriptor, this, ttlTimeProvider);
		keyValueStatesByName.put(stateDescriptor.getName(), kvState);
		publishQueryableStateIfEnabled(stateDescriptor, kvState);
	}
	return (S) kvState;
}
 
Example 2
Source File: AbstractKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * @see KeyedStateBackend
 */
@Override
@SuppressWarnings("unchecked")
public <N, S extends State, V> S getOrCreateKeyedState(
		final TypeSerializer<N> namespaceSerializer,
		StateDescriptor<S, V> stateDescriptor) throws Exception {
	checkNotNull(namespaceSerializer, "Namespace serializer");
	checkNotNull(keySerializer, "State key serializer has not been configured in the config. " +
			"This operation cannot use partitioned state.");

	InternalKvState<K, ?, ?> kvState = keyValueStatesByName.get(stateDescriptor.getName());
	if (kvState == null) {
		if (!stateDescriptor.isSerializerInitialized()) {
			stateDescriptor.initializeSerializerUnlessSet(executionConfig);
		}
		kvState = TtlStateFactory.createStateAndWrapWithTtlIfEnabled(
			namespaceSerializer, stateDescriptor, this, ttlTimeProvider);
		keyValueStatesByName.put(stateDescriptor.getName(), kvState);
		publishQueryableStateIfEnabled(stateDescriptor, kvState);
	}
	return (S) kvState;
}
 
Example 3
Source File: AbstractKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * @see KeyedStateBackend
 */
@Override
@SuppressWarnings("unchecked")
public <N, S extends State, V> S getOrCreateKeyedState(
		final TypeSerializer<N> namespaceSerializer,
		StateDescriptor<S, V> stateDescriptor) throws Exception {
	checkNotNull(namespaceSerializer, "Namespace serializer");
	checkNotNull(keySerializer, "State key serializer has not been configured in the config. " +
			"This operation cannot use partitioned state.");

	InternalKvState<K, ?, ?> kvState = keyValueStatesByName.get(stateDescriptor.getName());
	if (kvState == null) {
		if (!stateDescriptor.isSerializerInitialized()) {
			stateDescriptor.initializeSerializerUnlessSet(executionConfig);
		}
		kvState = TtlStateFactory.createStateAndWrapWithTtlIfEnabled(
			namespaceSerializer, stateDescriptor, this, ttlTimeProvider);
		keyValueStatesByName.put(stateDescriptor.getName(), kvState);
		publishQueryableStateIfEnabled(stateDescriptor, kvState);
	}
	return (S) kvState;
}
 
Example 4
Source File: QueryableStateClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the request result.
 * @param jobId                     JobID of the job the queryable state belongs to.
 * @param queryableStateName        Name under which the state is queryable.
 * @param key			            The key that the state we request is associated with.
 * @param namespace					The namespace of the state.
 * @param keyTypeInfo				The {@link TypeInformation} of the keys.
 * @param namespaceTypeInfo			The {@link TypeInformation} of the namespace.
 * @param stateDescriptor			The {@link StateDescriptor} of the state we want to query.
 * @return Future holding the immutable {@link State} object containing the result.
 */
private <K, N, S extends State, V> CompletableFuture<S> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final K key,
		final N namespace,
		final TypeInformation<K> keyTypeInfo,
		final TypeInformation<N> namespaceTypeInfo,
		final StateDescriptor<S, V> stateDescriptor) {

	Preconditions.checkNotNull(jobId);
	Preconditions.checkNotNull(queryableStateName);
	Preconditions.checkNotNull(key);
	Preconditions.checkNotNull(namespace);

	Preconditions.checkNotNull(keyTypeInfo);
	Preconditions.checkNotNull(namespaceTypeInfo);
	Preconditions.checkNotNull(stateDescriptor);

	TypeSerializer<K> keySerializer = keyTypeInfo.createSerializer(executionConfig);
	TypeSerializer<N> namespaceSerializer = namespaceTypeInfo.createSerializer(executionConfig);

	stateDescriptor.initializeSerializerUnlessSet(executionConfig);

	final byte[] serializedKeyAndNamespace;
	try {
		serializedKeyAndNamespace = KvStateSerializer
				.serializeKeyAndNamespace(key, keySerializer, namespace, namespaceSerializer);
	} catch (IOException e) {
		return FutureUtils.getFailedFuture(e);
	}

	return getKvState(jobId, queryableStateName, key.hashCode(), serializedKeyAndNamespace)
		.thenApply(stateResponse -> createState(stateResponse, stateDescriptor));
}
 
Example 5
Source File: QueryableStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the request result.
 * @param jobId                     JobID of the job the queryable state belongs to.
 * @param queryableStateName        Name under which the state is queryable.
 * @param key			            The key that the state we request is associated with.
 * @param namespace					The namespace of the state.
 * @param keyTypeInfo				The {@link TypeInformation} of the keys.
 * @param namespaceTypeInfo			The {@link TypeInformation} of the namespace.
 * @param stateDescriptor			The {@link StateDescriptor} of the state we want to query.
 * @return Future holding the immutable {@link State} object containing the result.
 */
private <K, N, S extends State, V> CompletableFuture<S> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final K key,
		final N namespace,
		final TypeInformation<K> keyTypeInfo,
		final TypeInformation<N> namespaceTypeInfo,
		final StateDescriptor<S, V> stateDescriptor) {

	Preconditions.checkNotNull(jobId);
	Preconditions.checkNotNull(queryableStateName);
	Preconditions.checkNotNull(key);
	Preconditions.checkNotNull(namespace);

	Preconditions.checkNotNull(keyTypeInfo);
	Preconditions.checkNotNull(namespaceTypeInfo);
	Preconditions.checkNotNull(stateDescriptor);

	TypeSerializer<K> keySerializer = keyTypeInfo.createSerializer(executionConfig);
	TypeSerializer<N> namespaceSerializer = namespaceTypeInfo.createSerializer(executionConfig);

	stateDescriptor.initializeSerializerUnlessSet(executionConfig);

	final byte[] serializedKeyAndNamespace;
	try {
		serializedKeyAndNamespace = KvStateSerializer
				.serializeKeyAndNamespace(key, keySerializer, namespace, namespaceSerializer);
	} catch (IOException e) {
		return FutureUtils.getFailedFuture(e);
	}

	return getKvState(jobId, queryableStateName, key.hashCode(), serializedKeyAndNamespace)
		.thenApply(stateResponse -> createState(stateResponse, stateDescriptor));
}
 
Example 6
Source File: QueryableStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the request result.
 * @param jobId                     JobID of the job the queryable state belongs to.
 * @param queryableStateName        Name under which the state is queryable.
 * @param key			            The key that the state we request is associated with.
 * @param namespace					The namespace of the state.
 * @param keyTypeInfo				The {@link TypeInformation} of the keys.
 * @param namespaceTypeInfo			The {@link TypeInformation} of the namespace.
 * @param stateDescriptor			The {@link StateDescriptor} of the state we want to query.
 * @return Future holding the immutable {@link State} object containing the result.
 */
private <K, N, S extends State, V> CompletableFuture<S> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final K key,
		final N namespace,
		final TypeInformation<K> keyTypeInfo,
		final TypeInformation<N> namespaceTypeInfo,
		final StateDescriptor<S, V> stateDescriptor) {

	Preconditions.checkNotNull(jobId);
	Preconditions.checkNotNull(queryableStateName);
	Preconditions.checkNotNull(key);
	Preconditions.checkNotNull(namespace);

	Preconditions.checkNotNull(keyTypeInfo);
	Preconditions.checkNotNull(namespaceTypeInfo);
	Preconditions.checkNotNull(stateDescriptor);

	TypeSerializer<K> keySerializer = keyTypeInfo.createSerializer(executionConfig);
	TypeSerializer<N> namespaceSerializer = namespaceTypeInfo.createSerializer(executionConfig);

	stateDescriptor.initializeSerializerUnlessSet(executionConfig);

	final byte[] serializedKeyAndNamespace;
	try {
		serializedKeyAndNamespace = KvStateSerializer
				.serializeKeyAndNamespace(key, keySerializer, namespace, namespaceSerializer);
	} catch (IOException e) {
		return FutureUtils.getFailedFuture(e);
	}

	return getKvState(jobId, queryableStateName, key.hashCode(), serializedKeyAndNamespace)
		.thenApply(stateResponse -> createState(stateResponse, stateDescriptor));
}