<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=MapGroupsWithState_and_flatMapGroupsWithState</id>
	<title>MapGroupsWithState and flatMapGroupsWithState - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=MapGroupsWithState_and_flatMapGroupsWithState"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=MapGroupsWithState_and_flatMapGroupsWithState&amp;action=history"/>
	<updated>2026-12-06T00:07:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=MapGroupsWithState_and_flatMapGroupsWithState&amp;diff=97&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@f2492a3</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=MapGroupsWithState_and_flatMapGroupsWithState&amp;diff=97&amp;oldid=prev"/>
		<updated>2026-11-23T00:34:52Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@f2492a3&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{DISPLAYTITLE:mapGroupsWithState and flatMapGroupsWithState}}&lt;br /&gt;
{{Infobox concept&lt;br /&gt;
| name            = mapGroupsWithState and flatMapGroupsWithState&lt;br /&gt;
| also_known_as   = Arbitrary stateful operations (version 1); &amp;lt;code&amp;gt;[flat]MapGroupsWithState&amp;lt;/code&amp;gt;; &amp;lt;code&amp;gt;applyInPandasWithState&amp;lt;/code&amp;gt; in Python&lt;br /&gt;
| field           = [[Stream processing]]&lt;br /&gt;
| key_parts       = Grouping key, user function, &amp;lt;code&amp;gt;GroupState&amp;lt;/code&amp;gt; object, &amp;lt;code&amp;gt;GroupStateTimeout&amp;lt;/code&amp;gt;, output mode, optional initial state&lt;br /&gt;
| implementations = [[Structured Streaming]] in [[Apache Spark]] 2.2 and later (Scala, Java); &amp;lt;code&amp;gt;applyInPandasWithState&amp;lt;/code&amp;gt; in Python from 3.4&lt;br /&gt;
| related         = [[transformWithState]], [[Watermark]], [[State store]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-22|the v2.2.0, v3.2.0, v3.5.0 and v4.0.0 to v4.3.0 tags of apache/spark (KeyValueGroupedDataset.scala, GroupState.scala, python/pyspark/sql/pandas/group_ops.py, python/pyspark/sql/connect/group.py, docs/streaming/apis-on-dataframes-and-datasets.md), the Spark 2.2.0, 3.4.0 and 4.0.0 to 4.3.0 release notes, a JIRA search for deprecation tickets, and SPARK-19067, SPARK-35897, SPARK-40434, SPARK-43084, SPARK-43511 and SPARK-50770}}&lt;br /&gt;
{{Status|active |note=Not deprecated in any Spark release up to 4.3.0: the methods carry no deprecation annotation and no release note or migration guide deprecates them. Since Spark 4.0 the documentation calls them legacy and encourages [[transformWithState]] for new stateful applications. |ref=&amp;lt;ref name=&amp;quot;ssguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html |title=Structured Streaming Programming Guide: APIs on DataFrames and Datasets (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
In [[Structured Streaming]], &amp;#039;&amp;#039;&amp;#039;mapGroupsWithState&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;flatMapGroupsWithState&amp;#039;&amp;#039;&amp;#039; are the original operators for arbitrary stateful processing: they call a user function for each grouping key with that key&amp;#039;s new values and a &amp;lt;code&amp;gt;GroupState&amp;lt;/code&amp;gt; object that holds the key&amp;#039;s state between micro-batches.&amp;lt;ref name=&amp;quot;gs&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/streaming/GroupState.scala |title=GroupState.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; They were added to [[Apache Spark]] in version 2.2.0 for the Scala and Java Dataset API, and Spark 3.4.0 added the Python counterpart &amp;lt;code&amp;gt;applyInPandasWithState&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;r220&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-2-2-0.html |title=Spark Release 2.2.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r340&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-4-0.html |title=Spark Release 3.4.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; Spark 4.0 introduced [[transformWithState]] as their successor, but as of Spark 4.3.0 none of these operators is deprecated.&amp;lt;ref name=&amp;quot;ssguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Status ==&lt;br /&gt;
Whether these operators are &amp;quot;deprecated&amp;quot; is a common question, and the primary sources answer it the same way: they are not.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Code (4.0.0 to 4.3.0):&amp;#039;&amp;#039;&amp;#039; The &amp;lt;code&amp;gt;mapGroupsWithState&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;flatMapGroupsWithState&amp;lt;/code&amp;gt; methods of &amp;lt;code&amp;gt;KeyValueGroupedDataset&amp;lt;/code&amp;gt;, in the shared API, the classic implementation and the [[Spark Connect]] client, have no &amp;lt;code&amp;gt;@deprecated&amp;lt;/code&amp;gt; annotation, and the PySpark &amp;lt;code&amp;gt;applyInPandasWithState&amp;lt;/code&amp;gt; docstring has no deprecation notice.&amp;lt;ref name=&amp;quot;kvgd&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/KeyValueGroupedDataset.scala |title=KeyValueGroupedDataset.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;groupops&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/pandas/group_ops.py |title=group_ops.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Release notes:&amp;#039;&amp;#039;&amp;#039; The Spark 4.0.0, 4.1.0, 4.2.0 and 4.3.0 release notes do not deprecate them.&amp;lt;ref name=&amp;quot;r400&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-0-0.html |title=Spark Release 4.0.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r410&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4.1.0.html |title=Spark Release 4.1.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r420&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-2-0.html |title=Spark Release 4.2.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r430&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-3-0.html |title=Spark Release 4.3.0 |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; The Structured Streaming migration guide lists no deprecation of them either.&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/docs/streaming/ss-migration-guide.md |title=Structured Streaming Migration Guide (docs/streaming/ss-migration-guide.md at v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Documentation:&amp;#039;&amp;#039;&amp;#039; The Spark 4.3.0 programming guide describes them as the &amp;quot;legacy&amp;quot; operators and says that since Spark 4.0 users are encouraged to use &amp;lt;code&amp;gt;transformWithState&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;ssguide&amp;quot; /&amp;gt; The transformWithState guide calls that operator their &amp;quot;next generation replacement&amp;quot;.&amp;lt;ref name=&amp;quot;twsguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/structured-streaming-transform-with-state.html |title=Structured Streaming Programming Guide: transformWithState (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Stability:&amp;#039;&amp;#039;&amp;#039; From Spark 2.2.0 to 3.5, the &amp;lt;code&amp;gt;GroupState&amp;lt;/code&amp;gt; interface was annotated &amp;lt;code&amp;gt;@Experimental&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;@Evolving&amp;lt;/code&amp;gt;. Spark 4.0.0 removed &amp;lt;code&amp;gt;@Experimental&amp;lt;/code&amp;gt; (SPARK-50770), so in Spark 4.x it carries only &amp;lt;code&amp;gt;@Evolving&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;gs220&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v2.2.0/sql/core/src/main/scala/org/apache/spark/sql/streaming/GroupState.scala |title=GroupState.scala (v2.2.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;jira50770&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-50770 |title=SPARK-50770: Removing package scope for transformWithState operator APIs |publisher=Apache Software Foundation JIRA |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In short: the operators are supported and maintained in Spark 4.3.0 and existing queries keep working. New stateful applications are pointed to [[transformWithState]].&lt;br /&gt;
&lt;br /&gt;
== How it works ==&lt;br /&gt;
A query groups a Dataset with &amp;lt;code&amp;gt;groupByKey&amp;lt;/code&amp;gt; and passes a function to one of the two operators. For each key that has new data in a trigger, Spark calls the function with the key, an iterator over the new values and the key&amp;#039;s &amp;lt;code&amp;gt;GroupState&amp;lt;/code&amp;gt;. The function reads the state with &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;getOption&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;exists&amp;lt;/code&amp;gt;, changes it with &amp;lt;code&amp;gt;update&amp;lt;/code&amp;gt;, and deletes it with &amp;lt;code&amp;gt;remove&amp;lt;/code&amp;gt;. For a batch Dataset, the function is called once per group with empty state, which makes the operators the same as &amp;lt;code&amp;gt;mapGroups&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;flatMapGroups&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two operators differ in their output:&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;mapGroupsWithState&amp;#039;&amp;#039;&amp;#039; returns exactly one record per call, and the query must use the Update output mode. Aggregations are not allowed in the same query.&amp;lt;ref name=&amp;quot;ssguide&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;flatMapGroupsWithState&amp;#039;&amp;#039;&amp;#039; returns any number of records, including none, and takes an output mode parameter, Append or Update. In Append mode, aggregations are allowed after the operator.&amp;lt;ref name=&amp;quot;ssguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The guide also says that neither operator can be used before or after a stream-stream join.&amp;lt;ref name=&amp;quot;ssguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timeouts ===&lt;br /&gt;
A query sets one timeout type for all groups with &amp;lt;code&amp;gt;GroupStateTimeout&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;NoTimeout&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ProcessingTimeTimeout&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;EventTimeTimeout&amp;lt;/code&amp;gt;. Each group then sets its own timeout with &amp;lt;code&amp;gt;setTimeoutDuration&amp;lt;/code&amp;gt; (processing time) or &amp;lt;code&amp;gt;setTimeoutTimestamp&amp;lt;/code&amp;gt; (event time, which needs a [[watermark]]). When the timeout passes, Spark calls the function for that group with no values and &amp;lt;code&amp;gt;hasTimedOut&amp;lt;/code&amp;gt; set to true. A timeout never fires early, but there is no upper bound on how late it fires, because it is checked only when a trigger runs. The timeout is cleared on every call, so the function must set it again each time.&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Initial state ===&lt;br /&gt;
Spark 3.2.0 added overloads that take an initial state as a &amp;lt;code&amp;gt;KeyValueGroupedDataset&amp;lt;/code&amp;gt;, to start a query with state loaded from elsewhere (SPARK-35897).&amp;lt;ref name=&amp;quot;kvgd&amp;quot; /&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-35897 |title=SPARK-35897: Support user defined initial state with flatMapGroupsWithState in Structured Streaming |publisher=Apache Software Foundation JIRA |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Language and Spark Connect support ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! API !! Language !! Added in !! Spark Connect support&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;KeyValueGroupedDataset.mapGroupsWithState&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;flatMapGroupsWithState&amp;lt;/code&amp;gt; || Scala, Java || 2.2.0&amp;lt;ref name=&amp;quot;r220&amp;quot; /&amp;gt; || 3.5.0 (SPARK-43511)&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-43511 |title=SPARK-43511: Implemented State APIs for Spark Connect Scala |publisher=Apache Software Foundation JIRA |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;GroupedData.applyInPandasWithState&amp;lt;/code&amp;gt; || Python (pandas) || 3.4.0 (SPARK-40434)&amp;lt;ref name=&amp;quot;r340&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;groupops&amp;quot; /&amp;gt; || 3.5.0 (SPARK-43084)&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-43084 |title=SPARK-43084: Add Python state API (applyInPandasWithState) and verify UDFs |publisher=Apache Software Foundation JIRA |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison with transformWithState ==&lt;br /&gt;
{{Main|transformWithState}}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! mapGroupsWithState / flatMapGroupsWithState !! transformWithState&lt;br /&gt;
|-&lt;br /&gt;
! Added in&lt;br /&gt;
| Spark 2.2.0&amp;lt;ref name=&amp;quot;r220&amp;quot; /&amp;gt;&lt;br /&gt;
| Spark 4.0.0&amp;lt;ref name=&amp;quot;r400&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! User code&lt;br /&gt;
| One function of key, values and &amp;lt;code&amp;gt;GroupState&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
| A stateful processor class with separate methods for input, timers, initial state and cleanup&amp;lt;ref name=&amp;quot;twsguide&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! State per key&lt;br /&gt;
| One object&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
| Any number of named value, list and map variables&amp;lt;ref name=&amp;quot;twsguide&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Timers&lt;br /&gt;
| One timeout per key, reset on every call&amp;lt;ref name=&amp;quot;gs&amp;quot; /&amp;gt;&lt;br /&gt;
| Many timers per key that persist until they fire or are deleted&amp;lt;ref name=&amp;quot;twsguide&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Automatic expiry (TTL)&lt;br /&gt;
| No&lt;br /&gt;
| Yes, per state variable, processing time only&amp;lt;ref name=&amp;quot;twsguide&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! State store providers&lt;br /&gt;
| HDFS-backed (default) or RocksDB&lt;br /&gt;
| RocksDB only&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/transformwithstate/TransformWithStateExecBase.scala |title=TransformWithStateExecBase.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Real-time Mode (4.3.0)&lt;br /&gt;
| &amp;lt;code&amp;gt;flatMapGroupsWithState&amp;lt;/code&amp;gt; not yet supported&amp;lt;ref name=&amp;quot;rtm&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/real-time-mode.html |title=Real-time Mode (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Scala and Java API supported&amp;lt;ref name=&amp;quot;rtm&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Deprecated?&lt;br /&gt;
| No&lt;br /&gt;
| No&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
A running count per word in Scala. &amp;lt;code&amp;gt;words&amp;lt;/code&amp;gt; is a streaming &amp;lt;code&amp;gt;Dataset[String]&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;spark.implicits._&amp;lt;/code&amp;gt; is imported; the query must be written in Update output mode.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;scala&amp;quot;&amp;gt;&lt;br /&gt;
import org.apache.spark.sql.streaming.{GroupState, GroupStateTimeout}&lt;br /&gt;
&lt;br /&gt;
val counts = words&lt;br /&gt;
  .groupByKey(identity)&lt;br /&gt;
  .mapGroupsWithState[Long, (String, Long)](GroupStateTimeout.NoTimeout) {&lt;br /&gt;
    (word: String, values: Iterator[String], state: GroupState[Long]) =&amp;gt;&lt;br /&gt;
      val updated = state.getOption.getOrElse(0L) + values.size&lt;br /&gt;
      state.update(updated)&lt;br /&gt;
      (word, updated)&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[transformWithState]]&lt;br /&gt;
* [[Structured Streaming]]&lt;br /&gt;
* [[Watermark]]&lt;br /&gt;
* [[Spark Streaming (DStreams)]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>