CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-flink--flink-parent

Apache Flink is an open source stream processing framework with powerful stream- and batch-processing capabilities

Pending
Overview
Eval results
Files

state-management.mddocs/

State Management

Comprehensive state management API supporting both synchronous and asynchronous operations. Flink provides different types of state for various use cases including value state, list state, map state, and specialized aggregating states for stateful stream processing applications.

Capabilities

State Interfaces (Synchronous API)

Core state interfaces for traditional synchronous state access.

/**
 * Base interface for all state types
 */
interface State {
    /**
     * Clear the state
     */
    void clear();
}

/**
 * State that holds a single value
 * @param <T> Type of the value
 */
interface ValueState<T> extends State {
    /**
     * Get current value
     * @return Current value, null if not set
     * @throws Exception
     */
    T value() throws Exception;
    
    /**
     * Update the state value
     * @param value New value
     * @throws Exception
     */
    void update(T value) throws Exception;
}

/**
 * State that holds a list of elements
 * @param <T> Type of elements in the list
 */
interface ListState<T> extends State {
    /**
     * Get all elements in the list
     * @return Iterable over all elements
     * @throws Exception
     */
    Iterable<T> get() throws Exception;
    
    /**
     * Add element to the list
     * @param value Element to add
     * @throws Exception
     */
    void add(T value) throws Exception;
    
    /**
     * Add all elements from iterable to the list
     * @param values Elements to add
     * @throws Exception
     */
    void addAll(List<T> values) throws Exception;
    
    /**
     * Replace all elements in the list
     * @param values New elements
     * @throws Exception
     */
    void update(List<T> values) throws Exception;
}

/**
 * State that holds a key-value map
 * @param <UK> Type of user keys
 * @param <UV> Type of user values
 */
interface MapState<UK, UV> extends State {
    /**
     * Get value for the given key
     * @param key User key
     * @return Value for the key, null if not present
     * @throws Exception
     */
    UV get(UK key) throws Exception;
    
    /**
     * Associate value with key
     * @param key User key
     * @param value User value
     * @throws Exception
     */
    void put(UK key, UV value) throws Exception;
    
    /**
     * Add all key-value pairs from map
     * @param map Key-value pairs to add
     * @throws Exception
     */
    void putAll(Map<UK, UV> map) throws Exception;
    
    /**
     * Remove key-value pair
     * @param key Key to remove
     * @throws Exception
     */
    void remove(UK key) throws Exception;
    
    /**
     * Check if key exists
     * @param key Key to check
     * @return true if key exists
     * @throws Exception
     */
    boolean contains(UK key) throws Exception;
    
    /**
     * Get all entries
     * @return Iterable over all entries
     * @throws Exception
     */
    Iterable<Map.Entry<UK, UV>> entries() throws Exception;
    
    /**
     * Get all keys
     * @return Iterable over all keys
     * @throws Exception
     */
    Iterable<UK> keys() throws Exception;
    
    /**
     * Get all values
     * @return Iterable over all values
     * @throws Exception
     */
    Iterable<UV> values() throws Exception;
    
    /**
     * Check if state is empty
     * @return true if no entries
     * @throws Exception
     */
    boolean isEmpty() throws Exception;
}

/**
 * State for pre-aggregating values using AggregateFunction
 * @param <IN> Input type
 * @param <OUT> Output type
 */
interface AggregatingState<IN, OUT> extends State {
    /**
     * Get current aggregated result
     * @return Aggregated result
     * @throws Exception
     */
    OUT get() throws Exception;
    
    /**
     * Add value to aggregation
     * @param value Value to add
     * @throws Exception
     */
    void add(IN value) throws Exception;
}

/**
 * State that reduces values on-the-fly using ReduceFunction
 * @param <T> Element type
 */
interface ReducingState<T> extends State {
    /**
     * Get current reduced result
     * @return Reduced result
     * @throws Exception
     */
    T get() throws Exception;
    
    /**
     * Add value to reduction
     * @param value Value to add
     * @throws Exception
     */
    void add(T value) throws Exception;
}

/**
 * State for broadcast patterns - read-only for non-broadcast stream
 * @param <K> Key type
 * @param <V> Value type
 */
interface BroadcastState<K, V> extends State {
    /**
     * Get value for key (read-only for non-broadcast stream)
     * @param key Key to lookup
     * @return Value for key
     * @throws Exception
     */
    V get(K key) throws Exception;
    
    /**
     * Check if key exists (read-only for non-broadcast stream)
     * @param key Key to check
     * @return true if key exists
     * @throws Exception
     */
    boolean contains(K key) throws Exception;
    
    /**
     * Get all entries (read-only for non-broadcast stream)
     * @return Iterable over all entries
     * @throws Exception
     */
    Iterable<Map.Entry<K, V>> entries() throws Exception;
    
    /**
     * Get all keys (read-only for non-broadcast stream)
     * @return Iterable over all keys
     * @throws Exception
     */
    Iterable<K> keys() throws Exception;
    
    /**
     * Get all values (read-only for non-broadcast stream)
     * @return Iterable over all values
     * @throws Exception
     */
    Iterable<V> values() throws Exception;
    
    // Write operations available only in broadcast stream processing functions
    
    /**
     * Associate value with key (broadcast stream only)
     * @param key Key
     * @param value Value
     * @throws Exception
     */
    void put(K key, V value) throws Exception;
    
    /**
     * Add all key-value pairs (broadcast stream only)
     * @param map Key-value pairs to add
     * @throws Exception
     */
    void putAll(Map<K, V> map) throws Exception;
    
    /**
     * Remove key-value pair (broadcast stream only)
     * @param key Key to remove
     * @throws Exception
     */
    void remove(K key) throws Exception;
}

State Descriptors

Descriptors for creating and configuring state variables.

/**
 * Base descriptor for state variables
 * @param <S> State type
 * @param <T> Value type
 */
abstract class StateDescriptor<S extends State, T> {
    /**
     * Get state name
     * @return State name
     */
    public String getName();
    
    /**
     * Get type information
     * @return Type information
     */
    public TypeInformation<T> getTypeInformation();
    
    /**
     * Set default value
     * @param defaultValue Default value
     */
    public void setDefaultValue(T defaultValue);
    
    /**
     * Get default value
     * @return Default value
     */
    public T getDefaultValue();
}

/**
 * Descriptor for ValueState
 * @param <T> Value type
 */
class ValueStateDescriptor<T> extends StateDescriptor<ValueState<T>, T> {
    /**
     * Create descriptor with name and type information
     * @param name State name
     * @param typeInfo Type information
     */
    public ValueStateDescriptor(String name, TypeInformation<T> typeInfo);
    
    /**
     * Create descriptor with name and type class
     * @param name State name
     * @param typeClass Type class
     */
    public ValueStateDescriptor(String name, Class<T> typeClass);
}

/**
 * Descriptor for ListState
 * @param <T> Element type
 */
class ListStateDescriptor<T> extends StateDescriptor<ListState<T>, List<T>> {
    /**
     * Create descriptor with name and element type information
     * @param name State name
     * @param elementTypeInfo Element type information
     */
    public ListStateDescriptor(String name, TypeInformation<T> elementTypeInfo);
    
    /**
     * Create descriptor with name and element type class
     * @param name State name
     * @param elementTypeClass Element type class
     */
    public ListStateDescriptor(String name, Class<T> elementTypeClass);
}

/**
 * Descriptor for MapState
 * @param <UK> User key type
 * @param <UV> User value type
 */
class MapStateDescriptor<UK, UV> extends StateDescriptor<MapState<UK, UV>, Map<UK, UV>> {
    /**
     * Create descriptor with name and type information
     * @param name State name
     * @param keyTypeInfo Key type information
     * @param valueTypeInfo Value type information
     */
    public MapStateDescriptor(String name, TypeInformation<UK> keyTypeInfo, TypeInformation<UV> valueTypeInfo);
    
    /**
     * Create descriptor with name and type classes
     * @param name State name
     * @param keyTypeClass Key type class
     * @param valueTypeClass Value type class
     */
    public MapStateDescriptor(String name, Class<UK> keyTypeClass, Class<UV> valueTypeClass);
}

/**
 * Descriptor for AggregatingState
 * @param <IN> Input type
 * @param <ACC> Accumulator type
 * @param <OUT> Output type
 */
class AggregatingStateDescriptor<IN, ACC, OUT> extends StateDescriptor<AggregatingState<IN, OUT>, ACC> {
    /**
     * Create descriptor with name, aggregate function, and accumulator type
     * @param name State name
     * @param aggFunction Aggregate function
     * @param accTypeInfo Accumulator type information
     */
    public AggregatingStateDescriptor(String name, AggregateFunction<IN, ACC, OUT> aggFunction, TypeInformation<ACC> accTypeInfo);
}

/**
 * Descriptor for ReducingState
 * @param <T> Element type
 */
class ReducingStateDescriptor<T> extends StateDescriptor<ReducingState<T>, T> {
    /**
     * Create descriptor with name, reduce function, and type information
     * @param name State name
     * @param reduceFunction Reduce function
     * @param typeInfo Type information
     */
    public ReducingStateDescriptor(String name, ReduceFunction<T> reduceFunction, TypeInformation<T> typeInfo);
}

State v2 API (Asynchronous)

Next-generation state API supporting asynchronous operations for improved performance.

/**
 * Base interface for async state API
 */
interface org.apache.flink.api.common.state.v2.State {
    /**
     * Clear the state asynchronously
     * @return Future representing completion
     */
    StateFuture<Void> asyncClear();
}

/**
 * Async single value state
 * @param <T> Value type
 */
interface org.apache.flink.api.common.state.v2.ValueState<T> extends org.apache.flink.api.common.state.v2.State {
    /**
     * Get current value asynchronously
     * @return Future with current value
     */
    StateFuture<T> asyncValue();
    
    /**
     * Update state value asynchronously
     * @param value New value
     * @return Future representing completion
     */
    StateFuture<Void> asyncUpdate(T value);
}

/**
 * Async list state
 * @param <T> Element type
 */
interface org.apache.flink.api.common.state.v2.ListState<T> extends org.apache.flink.api.common.state.v2.State {
    /**
     * Get all elements asynchronously
     * @return Future with iterable over elements
     */
    StateFuture<Iterable<T>> asyncGet();
    
    /**
     * Add element asynchronously
     * @param value Element to add
     * @return Future representing completion
     */
    StateFuture<Void> asyncAdd(T value);
    
    /**
     * Update list asynchronously
     * @param values New elements
     * @return Future representing completion
     */
    StateFuture<Void> asyncUpdate(List<T> values);
}

/**
 * Async map state
 * @param <UK> User key type
 * @param <UV> User value type
 */
interface org.apache.flink.api.common.state.v2.MapState<UK, UV> extends org.apache.flink.api.common.state.v2.State {
    /**
     * Get value for key asynchronously
     * @param key User key
     * @return Future with value
     */
    StateFuture<UV> asyncGet(UK key);
    
    /**
     * Put key-value pair asynchronously
     * @param key User key
     * @param value User value
     * @return Future representing completion
     */
    StateFuture<Void> asyncPut(UK key, UV value);
    
    /**
     * Remove key asynchronously
     * @param key Key to remove
     * @return Future representing completion
     */
    StateFuture<Void> asyncRemove(UK key);
    
    /**
     * Check if key exists asynchronously
     * @param key Key to check
     * @return Future with boolean result
     */
    StateFuture<Boolean> asyncContains(UK key);
    
    /**
     * Get all entries asynchronously
     * @return Future with iterable over entries
     */
    StateFuture<Iterable<Map.Entry<UK, UV>>> asyncEntries();
}

/**
 * Future type for async state operations
 * @param <T> Result type
 */
interface StateFuture<T> {
    /**
     * Apply function when future completes
     * @param fn Function to apply
     * @param <U> Function result type
     * @return New future with function result
     */
    <U> StateFuture<U> thenApply(Function<T, U> fn);
    
    /**
     * Compose with another async operation
     * @param fn Function returning another future
     * @param <U> Composed result type
     * @return Future representing composed operation
     */
    <U> StateFuture<U> thenCompose(Function<T, StateFuture<U>> fn);
    
    /**
     * Handle completion or exception
     * @param fn Handler function
     * @param <U> Handler result type
     * @return Future with handler result
     */
    <U> StateFuture<U> handle(BiFunction<T, Throwable, U> fn);
}

Watermark Management

Watermark system for event time processing.

/**
 * Base interface for watermarks
 */
interface Watermark {
    /**
     * Get watermark timestamp
     * @return Timestamp
     */
    long getTimestamp();
    
    /**
     * Check if this is a special watermark (e.g., MAX_WATERMARK)
     * @return true if special watermark
     */
    boolean isSpecial();
}

/**
 * Long-based watermark implementation
 */
class LongWatermark implements Watermark {
    /**
     * Create watermark with timestamp
     * @param timestamp Watermark timestamp
     */
    public LongWatermark(long timestamp);
    
    @Override
    public long getTimestamp();
    
    @Override
    public boolean isSpecial();
    
    /** Maximum possible watermark value */
    public static final LongWatermark MAX_WATERMARK;
}

/**
 * Boolean-based watermark implementation
 */
class BoolWatermark implements Watermark {
    /**
     * Create boolean watermark
     * @param value Boolean value
     */
    public BoolWatermark(boolean value);
    
    /**
     * Get boolean value
     * @return Boolean value
     */
    public boolean getValue();
}

/**
 * Interface for watermark management
 */
interface WatermarkManager {
    /**
     * Update watermarks
     * @param watermarks New watermarks
     */
    void updateWatermarks(Collection<Watermark> watermarks);
    
    /**
     * Get current combined watermark
     * @return Current watermark
     */
    Watermark getCurrentWatermark();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-flink--flink-parent

docs

configuration.md

connectors.md

core-functions.md

datastream-traditional.md

datastream-v2.md

index.md

state-management.md

table-api.md

windowing.md

tile.json