Documentation Index
Fetch the complete documentation index at: https://docs.streamnative.io/llms.txt
Use this file to discover all available pages before exploring further.
This feature is currently in private preview. If you want to try it out or have any questions, submit a ticket to the support team.
Configure state storage for Pulsar Functions
StreamNative Pulsar Functions support stateful functions that can maintain state across function invocations. This allows you to build more complex and powerful stream processing applications. It uses Oxia as a state storage interface. States are key-value pairs, where a key is a string and its value is arbitrary binary data - counters are stored as 64-bit big-endian binary values. Keys are scoped to an individual function and shared between instances of that function. To enable state storage for Pulsar Functions, you need to enable it explicitly when creating or updating a function by setting below arguments:State storage is only available for Java functions for now.
Call state APIs
Pulsar Functions expose below APIs for mutating and accessingstate.
The following table outlines the states that can be accessed within Java functions.
| State-related API | Java |
|---|---|
| Increment counter | incrCounter incrCounterAsync |
| Retrieve counter | getCounter getCounterAsync |
| Update state | putState putStateAsync |
| Retrieve state | getState getStateAsync |
| Delete state | deleteState |
Increment counter
UseincrCounter to increment the counter of a given key by the given amount.
If the key does not exist, a new key is created.
incrCounterAsync.
Retrieve counter
UsegetCounter to retrieve the counter of a given key mutated by incrCounter.
incrCounterAsync, you can use getCounterAsync.
Update state
Besides thecounter API, Pulsar also exposes a general key/value API for functions to store and update the state of a given key.
key, you can use putStateAsync.
Retrieve state
UsegetState to retrieve the state of a given key.
key, you can use getStateAsync.
Delete state
Query state via CLI
You can also query function state using CLI commands. This is useful for debugging and monitoring stateful functions.--watch is specified, the CLI tool keeps running to get the latest value of the provided state-key.
Example
The example ofWordCountFunction demonstrates how state is stored within Pulsar Functions.
-
The function splits the received
Stringinto multiple words using regex\\.. -
For each
word, the function incrementscounterby 1 viaincrCounter(key, amount).