<?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=Accumulator</id>
	<title>Accumulator - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Accumulator"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Accumulator&amp;action=history"/>
	<updated>2026-12-06T00:08:29Z</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=Accumulator&amp;diff=117&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Accumulator&amp;diff=117&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:31Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Accumulator&lt;br /&gt;
| also_known_as   = Spark accumulator, &amp;lt;code&amp;gt;AccumulatorV2&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Add-only updates from tasks, driver-only reads, merge of per-task updates, named accumulators in the UI&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Broadcast variable]], [[Speculative execution]], [[Spark Connect]], [[RDD]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/rdd-programming-guide.md, docs/web-ui.md, docs/spark-connect-overview.md, core util/AccumulatorV2.scala, sql metric/SQLMetrics.scala, python/pyspark/sql/observation.py, python/pyspark/sql/dataframe.py)}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], an &amp;#039;&amp;#039;&amp;#039;accumulator&amp;#039;&amp;#039;&amp;#039; is a shared variable that tasks can only add to, through an associative and commutative operation, and whose total only the driver can read. Accumulators are used for counters and sums across a job, such as counting bad records while parsing.&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#accumulators |title=RDD Programming Guide: Accumulators (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Spark guarantees that each task&amp;#039;s update is applied once only for updates made inside actions; updates made in transformations can be counted more than once.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; Accumulators are part of the &amp;lt;code&amp;gt;SparkContext&amp;lt;/code&amp;gt; API, which [[Spark Connect]] clients do not have.&amp;lt;ref name=&amp;quot;connect&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/spark-connect-overview.html |title=Spark Connect Overview (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How accumulators work ==&lt;br /&gt;
Tasks running on the cluster add to an accumulator with &amp;lt;code&amp;gt;add&amp;lt;/code&amp;gt; (or &amp;lt;code&amp;gt;+=&amp;lt;/code&amp;gt;) but cannot read it. Only the driver reads the value, with &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; Each task keeps its own local copy; when the task finishes, Spark merges the task&amp;#039;s updates into the driver&amp;#039;s accumulator. If the merge fails, Spark ignores the failure and still marks the task successful, so a faulty accumulator can be wrong even when the job succeeds.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark has built-in long and double accumulators (&amp;lt;code&amp;gt;SparkContext.longAccumulator()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doubleAccumulator()&amp;lt;/code&amp;gt; in Scala and Java, &amp;lt;code&amp;gt;SparkContext.accumulator()&amp;lt;/code&amp;gt; in Python). Custom types subclass &amp;lt;code&amp;gt;AccumulatorV2&amp;lt;/code&amp;gt; in Scala and Java, or &amp;lt;code&amp;gt;AccumulatorParam&amp;lt;/code&amp;gt; in Python, with &amp;lt;code&amp;gt;zero&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;addInPlace&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; The Spark guide names &amp;lt;code&amp;gt;reset&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;add&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt; as examples of the methods an &amp;lt;code&amp;gt;AccumulatorV2&amp;lt;/code&amp;gt; subclass has to override, and points to the API documentation for the rest; in Spark 4.3.0 the abstract methods are &amp;lt;code&amp;gt;isZero&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;copy&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;reset&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;add&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;accv2src&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala |title=AccumulatorV2.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A named accumulator appears in the Spark UI for each stage that modifies it, with per-task values in the tasks table; the docs note that this is not yet supported in Python.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;webui&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/web-ui.html |title=Web UI (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark uses the same mechanism internally. The metrics in the SQL tab, such as rows output or spill size, are implemented as &amp;lt;code&amp;gt;SQLMetric&amp;lt;/code&amp;gt;, a subclass of &amp;lt;code&amp;gt;AccumulatorV2&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;sqlmetrics&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/metric/SQLMetrics.scala |title=SQLMetrics.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
bad = spark.sparkContext.accumulator(0)&lt;br /&gt;
&lt;br /&gt;
def check(line):&lt;br /&gt;
    if not line.strip():&lt;br /&gt;
        bad.add(1)&lt;br /&gt;
&lt;br /&gt;
lines = spark.sparkContext.textFile(&amp;quot;s3a://bucket/raw/events.txt&amp;quot;)&lt;br /&gt;
lines.foreach(check)       # foreach is an action: each task&amp;#039;s update is applied once&lt;br /&gt;
print(bad.value)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Why can accumulators in transformations double count? ==&lt;br /&gt;
The Spark docs give two rules.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Inside actions&amp;#039;&amp;#039;&amp;#039; such as &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt;, Spark guarantees that each task&amp;#039;s update to an accumulator is applied only once; restarted tasks do not update the value again.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Inside transformations&amp;#039;&amp;#039;&amp;#039; such as &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, each task&amp;#039;s update may be applied more than once if tasks or stages are re-executed.&lt;br /&gt;
&lt;br /&gt;
Several things re-execute the work of a transformation:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Recomputation.&amp;#039;&amp;#039;&amp;#039; A DataFrame or RDD that is not cached is recomputed by every action that uses it, so an accumulator in a &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; counts once per action.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Retries and stage recomputation.&amp;#039;&amp;#039;&amp;#039; A failed task is retried, and a lost [[shuffle]] output makes Spark rerun the stage that produced it.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;[[Speculative execution]].&amp;#039;&amp;#039;&amp;#039; With &amp;lt;code&amp;gt;spark.speculation&amp;lt;/code&amp;gt; enabled, Spark launches a second copy of a slow task.&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Accumulators also follow lazy evaluation. An update inside &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; does not happen until an action computes the RDD, so reading the value before any action returns the initial value.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
seen = spark.sparkContext.accumulator(0)&lt;br /&gt;
mapped = lines.map(lambda l: (seen.add(1), l)[1])&lt;br /&gt;
&lt;br /&gt;
seen.value      # 0: no action has run yet&lt;br /&gt;
mapped.count()&lt;br /&gt;
mapped.count()  # not cached, so map runs again&lt;br /&gt;
seen.value      # twice the number of lines&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What replaces accumulators in DataFrame code and Spark Connect? ==&lt;br /&gt;
Spark Connect has no &amp;lt;code&amp;gt;SparkContext&amp;lt;/code&amp;gt;, so &amp;lt;code&amp;gt;SparkContext.accumulator&amp;lt;/code&amp;gt; is not available to Connect clients.&amp;lt;ref name=&amp;quot;connect&amp;quot; /&amp;gt; For DataFrames, &amp;lt;code&amp;gt;DataFrame.observe()&amp;lt;/code&amp;gt; with an &amp;lt;code&amp;gt;Observation&amp;lt;/code&amp;gt; computes named aggregate metrics, such as &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt;, on the data that flows through the DataFrame during an action, and reports them when the action ends.&amp;lt;ref name=&amp;quot;observation&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/observation.py |title=observation.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; &amp;lt;code&amp;gt;Observation&amp;lt;/code&amp;gt; was added to PySpark in 3.3.0, and &amp;lt;code&amp;gt;observe()&amp;lt;/code&amp;gt; supports Spark Connect since 3.5.0.&amp;lt;ref name=&amp;quot;observation&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;pydf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/dataframe.py |title=dataframe.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql import Observation&lt;br /&gt;
from pyspark.sql import functions as F&lt;br /&gt;
&lt;br /&gt;
obs = Observation(&amp;quot;quality&amp;quot;)&lt;br /&gt;
clean = raw.observe(obs, F.count(F.lit(1)).alias(&amp;quot;rows&amp;quot;),&lt;br /&gt;
                    F.sum(F.when(F.col(&amp;quot;id&amp;quot;).isNull(), 1).otherwise(0)).alias(&amp;quot;null_ids&amp;quot;))&lt;br /&gt;
clean.write.mode(&amp;quot;append&amp;quot;).saveAsTable(&amp;quot;main.bronze.events&amp;quot;)&lt;br /&gt;
print(obs.get)   # {&amp;#039;rows&amp;#039;: ..., &amp;#039;null_ids&amp;#039;: ...}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An &amp;lt;code&amp;gt;Observation&amp;lt;/code&amp;gt; collects its metrics during the first action only; later actions do not change the result. The class does not support streaming DataFrames.&amp;lt;ref name=&amp;quot;observation&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Broadcast variable]]&lt;br /&gt;
* [[Speculative execution]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
* [[RDD]]&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>