StateAccessor in YTsaurus Flow (Python)
Use StateAccessor to read, modify, and delete state values. For general information about stateful processing, see the Stateful processing section.
How it works
In Flow, the state is stored in sorted dynamic tables. If you’re using external state, you create this table yourself. If you’re using internal state, Flow automatically creates and manages the tables.
For TTransformCompanionComputation, the key columns in the state table match the group_by_schema of the computation. For the internal state of TTransformOrderedSourceCompanionComputation, the source partition key is used instead; this SourceComputation does not support group_by_schema. In both cases, messages with the same key share one state.
Reading and writing data
The worker directly handles table operations (reading, writing, and deleting data). When the worker receives a new batch of messages, it loads the state values for all keys in the batch and sends them to the companion along with the messages and timers. For more details, see the interaction schema.
You write new values to the state table transactionally within an epoch.
Accessor types
Choose an accessor for the state location and format:
|
Accessor |
Format |
Retrieval |
Description |
|
YSON (dict) |
|
Serializes a Python dict to YSON |
|
|
|
|
Raw bytes without serialization |
|
|
Protobuf |
|
Serializes using Protobuf |
|
|
Payload (table row) |
|
Typed access to an external table |
|
|
Payload (table row) |
|
Read-only access through a row-format joiner |
|
|
Protobuf |
|
Explicit snapshot writes through a protobuf-capable manager |
|
|
Protobuf |
|
Read-only snapshots through a protobuf-capable joiner |
The first three accessors work with internal state (tables are automatically managed by Flow). External accessors use a configured manager or joiner and a table you create yourself. Their format is determined by that implementation; choosing a Python accessor does not change it. See the protobuf prerequisites and availability.
Common internal-state API
All internal accessors (YsonStateAccessor, RawStateAccessor, ProtoStateAccessor) provide the same set of methods:
|
Method |
Description |
|
|
Get the state value (or |
|
|
Set the state value |
|
|
Delete the state for the current key |
|
|
Get the value or return |
Internal accessors also provide read_only(). Their values are live and shared within a batch. External accessors have a separate contract: row access reads fields through Payload, and protobuf access returns independent message snapshots that require explicit set() calls. Joined external accessors reject writes.
Getting an accessor
You get an accessor through RuntimeContext (ctx) inside on_message or on_timer:
class MyFunction(RowFunction):
def on_message(self, message, output, ctx):
# YSON (dict)
yson_state = ctx.state("state-name", message)
# Raw bytes
raw_state = ctx.raw_state("state-name", message)
# Protobuf
proto_state = ctx.proto_state("state-name", message, MyProtoClass)
# External (the name must start with "/")
ext_state = ctx.external_state("/state-name", message)
def on_timer(self, timer, output, ctx):
# Similarly, but pass timer instead of message
state = ctx.state("state-name", timer)
Parameters:
name— a string with the state name declared in the static spec. For internal states, this is a string frominternal_states; for external states, it is a key fromexternal_state_managers, orexternal_state_joinersfor joined access, and must start with/.message/timer— the message or timer for which you need to get the state for the key.ProtoClass(forproto_state,proto_external_state, andjoined_proto_external_state) — the Protobuf message class for deserialization. For external state, its descriptor name must match the manager or joiner'sproto_type.
When to use which type
|
Situation |
Recommended accessor |
|
A simple dictionary with several fields |
|
|
Arbitrary binary data |
|
|
Structured data with a fixed schema |
|
|
Data that needs to be accessed from other systems |
|
|
Data that requires a custom table |
|
|
A row-format lookup without writes |
|
|
An external manager that exposes protobuf state |
|
|
A protobuf lookup through a compatible joiner |
|
See also
- Internal State (Python)
- External State (Python)
- Working with states (Python) — a brief overview of all types
- Stateful processing (concept)
- StateAccessor (Java)