home
diamond Go Premium
Data Engineering Path  ·  PySpark

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, use saveAsTextFile() or preview data using take(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']
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.