Package org.apache.flink.runtime.state
Interface KeyedStateBackend<K>
-
- Type Parameters:
K- The key by which state is keyed.
- All Superinterfaces:
org.apache.flink.util.Disposable,KeyedStateFactory,PriorityQueueSetFactory
- All Known Subinterfaces:
CheckpointableKeyedStateBackend<K>,TestableKeyedStateBackend<K>
- All Known Implementing Classes:
AbstractKeyedStateBackend,BatchExecutionKeyedStateBackend,HeapKeyedStateBackend
public interface KeyedStateBackend<K> extends KeyedStateFactory, PriorityQueueSetFactory, org.apache.flink.util.Disposable
A keyed state backend provides methods for managing keyed state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceKeyedStateBackend.KeySelectionListener<K>Listener is given a callback whensetCurrentKey(K)is called (key context changes).
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <N,S extends org.apache.flink.api.common.state.State,T>
voidapplyToAllKeys(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor, KeyedStateFunction<K,S> function)Applies the providedKeyedStateFunctionto the state with the providedStateDescriptorof all the currently active keys.booleanderegisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)Stop calling listener registered inregisterKeySelectionListener(org.apache.flink.runtime.state.KeyedStateBackend.KeySelectionListener<K>).voiddispose()KgetCurrentKey()<N> Stream<K>getKeys(String state, N namespace)<N> Stream<K>getKeys(List<String> states, N namespace)<N> Stream<org.apache.flink.api.java.tuple.Tuple2<K,N>>getKeysAndNamespaces(String state)org.apache.flink.api.common.typeutils.TypeSerializer<K>getKeySerializer()<N,S extends org.apache.flink.api.common.state.State,T>
SgetOrCreateKeyedState(org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor)Creates or retrieves a keyed state backed by this state backend.<N,S extends org.apache.flink.api.common.state.State>
SgetPartitionedState(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,?> stateDescriptor)Creates or retrieves a partitioned state backed by this state backend.default booleanisSafeToReuseKVState()Whether it's safe to reuse key-values from the state-backend, e.g for the purpose of optimization.voidregisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)State backend will callKeyedStateBackend.KeySelectionListener.keySelected(K)when key context is switched if supported.voidsetCurrentKey(K newKey)Sets the current key that is used for partitioned state.voidsetCurrentKeyAndKeyGroup(K newKey, int newKeyGroupIndex)Act as a fast path forsetCurrentKey(K)when the key group is known.-
Methods inherited from interface org.apache.flink.runtime.state.KeyedStateFactory
createOrUpdateInternalState, createOrUpdateInternalState, createOrUpdateInternalState
-
Methods inherited from interface org.apache.flink.runtime.state.PriorityQueueSetFactory
create, create
-
-
-
-
Method Detail
-
setCurrentKey
void setCurrentKey(K newKey)
Sets the current key that is used for partitioned state.- Parameters:
newKey- The new current key.
-
getCurrentKey
K getCurrentKey()
- Returns:
- Current key.
-
setCurrentKeyAndKeyGroup
void setCurrentKeyAndKeyGroup(K newKey, int newKeyGroupIndex)
Act as a fast path forsetCurrentKey(K)when the key group is known.
-
getKeySerializer
org.apache.flink.api.common.typeutils.TypeSerializer<K> getKeySerializer()
- Returns:
- Serializer of the key.
-
applyToAllKeys
<N,S extends org.apache.flink.api.common.state.State,T> void applyToAllKeys(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor, KeyedStateFunction<K,S> function) throws ExceptionApplies the providedKeyedStateFunctionto the state with the providedStateDescriptorof all the currently active keys.- Type Parameters:
N- The type of the namespace.S- The type of the state.- Parameters:
namespace- the namespace of the state.namespaceSerializer- the serializer for the namespace.stateDescriptor- the descriptor of the state to which the function is going to be applied.function- the function to be applied to the keyed state.- Throws:
Exception
-
getKeys
<N> Stream<K> getKeys(String state, N namespace)
- Parameters:
state- State variable for which existing keys will be returned.namespace- Namespace for which existing keys will be returned.- Returns:
- A stream of all keys for the given state and namespace. Modifications to the state during iterating over its keys are not supported.
-
getKeys
<N> Stream<K> getKeys(List<String> states, N namespace)
- Parameters:
states- State variables for which existing keys will be returned.namespace- Namespace for which existing keys will be returned.- Returns:
- A stream of all keys for the multiple states and a given namespace. Modifications to the states during iterating over its keys are not supported.
-
getKeysAndNamespaces
<N> Stream<org.apache.flink.api.java.tuple.Tuple2<K,N>> getKeysAndNamespaces(String state)
- Parameters:
state- State variable for which existing keys will be returned.- Returns:
- A stream of all keys for the given state and namespace. Modifications to the state during iterating over it keys are not supported. Implementations go not make any ordering guarantees about the returned tupes. Two records with the same key or namespace may not be returned near each other in the stream.
-
getOrCreateKeyedState
<N,S extends org.apache.flink.api.common.state.State,T> S getOrCreateKeyedState(org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor) throws ExceptionCreates or retrieves a keyed state backed by this state backend.- Type Parameters:
N- The type of the namespace.S- The type of the state.- Parameters:
namespaceSerializer- The serializer used for the namespace type of the statestateDescriptor- The identifier for the state. This contains name and can create a default state value.- Returns:
- A new key/value state backed by this backend.
- Throws:
Exception- Exceptions may occur during initialization of the state and should be forwarded.
-
getPartitionedState
<N,S extends org.apache.flink.api.common.state.State> S getPartitionedState(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,?> stateDescriptor) throws ExceptionCreates or retrieves a partitioned state backed by this state backend.TODO: NOTE: This method does a lot of work caching / retrieving states just to update the namespace. This method should be removed for the sake of namespaces being lazily fetched from the keyed state backend, or being set on the state directly.
- Type Parameters:
N- The type of the namespace.S- The type of the state.- Parameters:
stateDescriptor- The identifier for the state. This contains name and can create a default state value.- Returns:
- A new key/value state backed by this backend.
- Throws:
Exception- Exceptions may occur during initialization of the state and should be forwarded.
-
dispose
void dispose()
- Specified by:
disposein interfaceorg.apache.flink.util.Disposable
-
registerKeySelectionListener
void registerKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
State backend will callKeyedStateBackend.KeySelectionListener.keySelected(K)when key context is switched if supported.
-
deregisterKeySelectionListener
boolean deregisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
Stop calling listener registered inregisterKeySelectionListener(org.apache.flink.runtime.state.KeyedStateBackend.KeySelectionListener<K>).- Returns:
- returns true iff listener was registered before.
-
isSafeToReuseKVState
default boolean isSafeToReuseKVState()
Whether it's safe to reuse key-values from the state-backend, e.g for the purpose of optimization.NOTE: this method should not be used to check for
InternalPriorityQueue, as the priority queue could be stored on different locations, e.g RocksDB state-backend could store that on JVM heap if configuring HEAP as the time-service factory.- Returns:
- returns ture if safe to reuse the key-values from the state-backend.
-
-