Accumulator variables help you count or sum things across many computers working together.
Accumulator variables in Apache Spark
from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() accum = spark.sparkContext.accumulator(0) rdd = spark.sparkContext.parallelize([1, 2, 3, 4]) rdd.foreach(lambda x: accum.add(x)) print(accum.value)
Use spark.sparkContext.accumulator(initial_value) to create an accumulator.
Use accum.add(value) inside actions like foreach to update it.
accum = spark.sparkContext.accumulator(0) rdd.foreach(lambda x: accum.add(1)) print(accum.value)
accum = spark.sparkContext.accumulator(0) rdd.foreach(lambda x: accum.add(x)) print(accum.value)
accum = spark.sparkContext.accumulator(0) rdd.filter(lambda x: x % 2 == 0).foreach(lambda x: accum.add(1)) print(accum.value)
This program counts how many times the word 'error' appears in the data using an accumulator.
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('AccumulatorExample').getOrCreate() # Create an accumulator starting at 0 error_count = spark.sparkContext.accumulator(0) # Sample data with some 'error' strings data = ['ok', 'error', 'ok', 'error', 'error', 'ok'] rdd = spark.sparkContext.parallelize(data) # Increase accumulator for each 'error' rdd.foreach(lambda x: error_count.add(1) if x == 'error' else None) print(f"Number of errors: {error_count.value}") spark.stop()
Accumulators only update correctly inside actions like foreach, not transformations.
Accumulator values are only reliable after the action completes.
They are write-only in tasks and readable only on the driver program.
Accumulator variables help count or sum values across many computers.
Create them with sparkContext.accumulator() and update inside actions.
Use them to track progress, errors, or statistics during Spark jobs.
Practice
Solution
Step 1: Understand accumulator role
Accumulators are special variables used to aggregate information like counts or sums across many worker nodes.Step 2: Compare with other options
Options B, C, and D describe caching, storage, or schema tasks, which are unrelated to accumulators.Final Answer:
To count or sum values across multiple nodes during a Spark job -> Option DQuick Check:
Accumulator = distributed counting/summing [OK]
- Confusing accumulators with broadcast variables
- Thinking accumulators cache data
- Assuming accumulators define data schema
Solution
Step 1: Recall accumulator creation syntax
The correct method to create an accumulator is sparkContext.accumulator(initial_value).Step 2: Check each option
accum = sparkContext.accumulator(0) uses the correct method and initial value. Options B, C, and D use incorrect method names or missing parameters.Final Answer:
accum = sparkContext.accumulator(0) -> Option BQuick Check:
Use .accumulator(initial_value) to create [OK]
- Using .accumulate() instead of .accumulator()
- Omitting the initial value
- Using non-existent methods like createAccumulator
accum = sc.accumulator(0) rdd = sc.parallelize([1, 2, 3, 4]) rdd.foreach(lambda x: accum.add(x)) print(accum.value)
What will be the output?
Solution
Step 1: Understand accumulator usage in foreach
The foreach action applies the lambda to each element, adding each value to the accumulator.Step 2: Calculate the sum of elements
Sum of [1, 2, 3, 4] is 10, so accum.value will be 10 after foreach completes.Final Answer:
10 -> Option CQuick Check:
Sum of list = accumulator value = 10 [OK]
- Expecting accumulator to update inside transformations
- Thinking accumulator value stays zero without action
- Confusing accumulator with variable inside map
accum = sc.accumulator(0) rdd = sc.parallelize([1, 2, 3]) rdd.map(lambda x: accum.add(x)) print(accum.value)
Solution
Step 1: Identify accumulator usage in map
map is a transformation and lazy; accumulator updates inside map may not execute unless an action triggers it.Step 2: Understand why accumulator value remains unchanged
Since no action is called, accum.value remains 0 or unchanged, so this code does not update accumulator as expected.Final Answer:
Accumulator updates inside map are not guaranteed to run -> Option AQuick Check:
Accumulator updates must be inside actions [OK]
- Expecting accumulator to update inside lazy transformations
- Initializing accumulator with wrong data type
- Printing accumulator without triggering action
Solution
Step 1: Identify correct accumulator update inside action
accum = sc.accumulator(0) rdd.filter(lambda x: x > 5).foreach(lambda x: accum.add(1)) print(accum.value)
filters numbers > 5, then uses foreach (an action) to add 1 to accumulator for each such number.Step 2: Check other options for errors
accum = sc.accumulator(0) rdd.foreach(lambda x: accum.add(1) if x > 5 else None) print(accum.value)
uses foreach with correct syntax; the originalaccum = sc.accumulator(0) rdd.foreach(lambda x: accum.add(1) if x > 5 else None) print(accum.value)
had a syntax error missing else clause, corrected here;accum = sc.accumulator(0) rdd.map(lambda x: accum.add(1) if x > 5 else 0) print(accum.value)
uses map (lazy transformation) so accumulator won't update;accum = sc.accumulator(0) rdd.reduce(lambda x, y: accum.add(1) if x > 5 else 0) print(accum.value)
misuses reduce which is not for side effects.Final Answer:
accum = sc.accumulator(0) rdd.filter(lambda x: x > 5).foreach(lambda x: accum.add(1)) print(accum.value) -> Option AQuick Check:
Use filter + foreach to update accumulator correctly [OK]
- Updating accumulator inside map or reduce instead of foreach
- Incorrect lambda syntax for conditional add
- Not triggering an action to update accumulator
Ready to prove it?
Pass the test and this pattern moves unknown
