RDD - Action Collect
The collect() action is one of the most widely used operations for retrieving results in Apache Spark. It fetches every single record from all partition datasets scattered across the executor nodes and merges them into a single Python list on the Driver program.
️ Critical Danger: Out of Memory (OOM)
Because collect() brings all distributed records into the memory (RAM) of the single Driver machine, it presents a significant risk in production:
- The Problem: If your distributed dataset is 100GB, and your Driver JVM is configured with only 8GB of RAM, your Spark application will instantly crash with an
OutOfMemoryError! - Best Practice: Only call
collect()on small aggregated datasets, filter results, or configurations. For large scale outputs, usesaveAsTextFile()or preview data usingtake(n).
graph TD
subgraph Executors["Executors (Worker Nodes)"]
E1["Executor 1 (40GB data)"]
E2["Executor 2 (40GB data)"]
end
subgraph DriverNode["Driver Program (Master Node)"]
DP["Driver RAM: 8GB"]
end
E1 -->|collect() transfers ALL data| DP
E2 -->|collect() transfers ALL data| DP
DP -->|Memory Saturation| Crash["OutOfMemoryError (OOM) / CRASH!"]
style Executors fill:#efebe9,stroke:#8d6e63,stroke-width:2px;
style DriverNode fill:#ffebee,stroke:#c62828,stroke-width:2px;
style Crash fill:#ffebee,stroke:#c62828,stroke-width:2px;
PySpark Code Example
Setup Spark Session
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("RDD Action Collect") \
.master("local[*]") \
.getOrCreate()
sc = spark.sparkContext
In Action: Fetching Data
Let's see how collect() gathers distributed partition strings back to the Driver:
# 1. Parallelize a small list (distributed across nodes)
languages_rdd = sc.parallelize(["Python", "Scala", "Java", "R"], numSlices=2)
# 2. Trigger the action to fetch elements back
local_list = languages_rdd.collect()
# 3. Print the retrieved type and values inside the driver terminal
print("Collected Result Type :", type(local_list)) # <class 'list'>
print("Collected Result Items:", local_list)
# Output: Collected Result Items: ['Python', 'Scala', 'Java', 'R']