Accumulators in PySpark
Introduction:
Accumulators in PySpark are variables that are used to aggregate information across executors. They provide a simple way to collect metrics and other values during the execution of your Spark jobs. Unlike broadcast variables, which are read-only, accumulators can be updated by multiple tasks and used to collect statistics or debugging information.
What is an Accumulator?
An accumulator is a shared variable that can be added to by tasks running on different nodes in a cluster. The value of an accumulator is only accessible on the driver node, and it’s typically used to count events or sum values across the entire dataset.
How to Define an Accumulator?
You can define an accumulator in PySpark using the `accumulator` method available on the `SparkContext`. Here’s a simple example:
— — — — — — — — — — — — — -python
from pyspark.sql import SparkSession
Initialize Spark session
spark = SparkSession.builder.appName(‘Accumulator Example’).getOrCreate()
Initialize an accumulator
acc = spark.sparkContext.accumulator(0)
— — — — — — — — — — — — — -
In this example:
- We create a Spark session.
- We initialize an accumulator with an initial value of 0.
Real-Life Scenario: Counting Error Messages in Log Data
Imagine you are processing logs from multiple servers to find out how many error messages occurred during a certain period. You want to keep track of the number of error messages using an accumulator.
Steps:
1. Initialize the Spark session and accumulators.
2. Read log data into a DataFrame.
3. Use accumulators to count error messages.
4. Perform transformations and actions on the DataFrame.
5. Output the results.
Example Code
Get DataWithSantosh’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s implement this scenario step-by-step:
Explanation
1. Initialization:
— Create a Spark session.
— Initialize an accumulator `error_count_accumulator` to count error messages.
2. Sample Data:
— A sample DataFrame `df` is created with log data including timestamps, log levels, and messages.
3. Counting Errors:
— Define a function `count_errors` that increments the accumulator for each row with a log level of “ERROR”.
— Use the `foreach` action to apply this function to each row in the DataFrame.
4. Output:
— Print the total count of error messages using the value of the accumulator.
— Further process the DataFrame to filter and display only the error logs.
Visual Representation
1. Driver Node:
— Initialization: Creates the `error_count_accumulator`.
— Broadcasting: Broadcasts the log data to executor nodes.
2. Executor Nodes:
— Task Execution: Each executor processes its partition of the DataFrame.
— Updating Accumulator: Tasks update the accumulator if an error log is found.
3. Aggregation:
— Driver Node: Aggregates updates from all tasks and combines them into the final count.
Why Use Accumulators in ETL Processes?
- Efficiency: Accumulators provide an efficient way to aggregate values across a distributed cluster without excessive data shuffling.
- Monitoring: Useful for tracking metrics such as error counts, processed records, or any custom counters during ETL operations.
- Fault Tolerance: Spark ensures the correctness of accumulator updates even in the event of task failures, recomputing the updates as needed.
Note
If this article helped you gain some knowledge, please clap and comment. Don’t forget to follow me on Medium . Your support helps me create more content like this and keeps us connected in the data engineering community. Thank you!






