From 210cea51602cfb9dfa3ee8b0d5c997293270cbee Mon Sep 17 00:00:00 2001 From: Yuchen Liu Date: Fri, 12 Jul 2024 07:36:17 +0900 Subject: [PATCH] [SPARK-48850][DOCS][SS][SQL] Add documentation for new options added to State Data Source ### What changes were proposed in this pull request? In https://github.com/apache/spark/pull/46944 and https://github.com/apache/spark/pull/47188, we introduced some new options to the State Data Source. This PR aims to explain these new features in the documentation. ### Why are the changes needed? It is necessary to reflect the latest change in the documentation website. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? The API Doc website can be rendered correctly. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #47274 from eason-yuchen-liu/snapshot-doc. Authored-by: Yuchen Liu Signed-off-by: Jungtaek Lim --- .../structured-streaming-state-data-source.md | 115 +++++++++++++++++- 1 file changed, 112 insertions(+), 3 deletions(-) diff --git a/docs/structured-streaming-state-data-source.md b/docs/structured-streaming-state-data-source.md index 9866991306690..1fac39566c1da 100644 --- a/docs/structured-streaming-state-data-source.md +++ b/docs/structured-streaming-state-data-source.md @@ -42,7 +42,7 @@ Users can read an instance of state store, which is matched to a single stateful Note that there could be an exception, e.g. stream-stream join, which leverages multiple state store instances internally. The data source abstracts the internal representation away from users and provides a user-friendly approach to read the state. See the section for stream-stream join for more details. -### Creating a State store for Batch Queries (all defaults) +### Creating a state store for batch queries (all defaults)
@@ -144,9 +144,40 @@ The following configurations are optional: (none) Represents the target side to read from. This option is used when users want to read the state from stream-stream join. + + snapshotStartBatchId + numeric value + + If specified, force to read the snapshot at this batch ID, then changelogs will be replayed until 'batchId' or its default. Note that snapshot batch ID starts with 0 and equals to snapshot version ID minus 1. This option must be used together with 'snapshotPartitionId'. + + + snapshotPartitionId + numeric value + + If specified, only this specific partition will be read. Note that partition ID starts with 0. This option must be used together with 'snapshotStartBatchId'. + + + readChangeFeed + boolean + false + If set to true, will read the change of state over microbatches. The output table schema will also differ. Details can be found in section "Reading state changes over microbatches". Option 'changeStartBatchId' must be specified with this option. Option 'batchId', 'joinSide', 'snapshotStartBatchId' and 'snapshotPartitionId' cannot be used together with this option. + + + changeStartBatchId + numeric value + + Represents the first batch to read in the read change feed mode. This option requires 'readChangeFeed' to be set to true. + + + changeEndBatchId + numeric value + latest commited batchId + Represents the last batch to read in the read change feed mode. This option requires 'readChangeFeed' to be set to true. + -### Reading state for Stream-stream join + +### Reading state for stream-stream join Structured Streaming implements the stream-stream join feature via leveraging multiple instances of state store internally. These instances logically compose buffers to store the input rows for left and right. @@ -154,6 +185,85 @@ These instances logically compose buffers to store the input rows for left and r Since it is more obvious to users to reason about, the data source provides the option 'joinSide' to read the buffered input for specific side of the join. To enable the functionality to read the internal state store instance directly, we also allow specifying the option 'storeName', with restriction that 'storeName' and 'joinSide' cannot be specified together. +### Reading state changes over microbatches + +If we want to understand the change of state store over microbatches instead of the whole state store at a particular microbatch, 'readChangeFeed' is the option to use. +For example, this is the code to read the change of state from batch 2 to the latest committed batch. + +
+ +
+{% highlight python %} + +df = spark \ +.read \ +.format("statestore") \ +.option("readChangeFeed", true) \ +.option("changeStartBatchId", 2) \ +.load("") + +{% endhighlight %} +
+ +
+{% highlight scala %} + +val df = spark +.read +.format("statestore") +.option("readChangeFeed", true) +.option("changeStartBatchId", 2) +.load("") + +{% endhighlight %} +
+ +
+{% highlight java %} + +Dataset df = spark +.read() +.format("statestore") +.option("readChangeFeed", true) +.option("changeStartBatchId", 2) +.load(""); + +{% endhighlight %} +
+ +
+ +The output schema will also be different from the normal output. + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ColumnTypeNote
batch_idlong
change_typestringThere are two possible values: 'update' and 'delete'. Update represents either inserting a non-existing key-value pair or updating an existing key with new value. The 'value' field will be null for delete records.
keystruct (depends on the type for state key)
valuestruct (depends on the type for state value)
partition_idint
+ ## State metadata source Before querying the state from existing checkpoint via state data source, users would like to understand the information for the checkpoint, especially about state operator. This includes which operators and state store instances are available in the checkpoint, available range of batch IDs, etc. @@ -179,7 +289,6 @@ df = spark \
{% highlight scala %} - val df = spark .read .format("state-metadata")