Apache Spark 101: Distributed Computing for Data Science write a full content on this topic

When datasets fit within a single machine’s RAM, libraries like Pandas, NumPy, and Scikit-Learn perform exceptionally well. However, as data scales into hundreds of gigabytes or terabytes, single-node processing fails due to memory limits and execution bottlenecks.
Apache Spark is an open-source, multi-language engine designed to execute data engineering, data science, and machine learning workloads across distributed computer clusters.

1. What is Distributed Computing?

Instead of processing a massive dataset on one expensive machine with high RAM and CPU cores, distributed computing splits data across a cluster of multiple interconnected commodity machines (nodes).
Single-Node Processing (Pandas / RAM Bottleneck):
┌─────────────────────────────────────────┐
│        Single Workstation (RAM: 16GB)   │  ──► Fails on 100GB Datasets!
└─────────────────────────────────────────┘

Distributed Cluster Processing (Apache Spark):
┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│ Worker Node 1│     │ Worker Node 2│     │ Worker Node 3│
│ (Splits 1-33)│     │(Splits 34-66)│     │(Splits 67-100│  ──► Scales horizontally
└──────────────┘     └──────────────┘     └──────────────┘      across hundreds of nodes

Why Apache Spark?

  • Speed via In-Memory Computing: Unlike Hadoop MapReduce, which writes intermediate calculation steps back to physical disk, Spark keeps intermediate results in RAM, making it up to 100x faster for iterative algorithms.
  • Unified Analytics Stack: Provides unified libraries for SQL queries (Spark SQL), streaming (Structured Streaming), machine learning (MLlib), and graph processing (GraphX).

2. Apache Spark Architecture

Spark operates using a classic Master-Worker architecture managed by a cluster manager (such as YARN, Kubernetes, or Spark’s Standalone manager).
                       ┌────────────────────────────────────────┐
                       │             DRIVER NODE                │
                       │  • SparkSession & DAG Scheduler        │
                       │  • Converts code to Execution Plan     │
                       └───────────────────┬────────────────────┘
                                           │
                                  Cluster Manager
                                           │
         ┌─────────────────────────────────┼─────────────────────────────────┐
         ▼                                 ▼                                 ▼
┌──────────────────┐              ┌──────────────────┐              ┌──────────────────┐
│  Worker Node 1   │              │  Worker Node 2   │              │  Worker Node 3   │
│ ┌──────────────┐ │              │ ┌──────────────┐ │              │ ┌──────────────┐ │
│ │ Executor 1   │ │              │ │ Executor 2   │ │              │ │ Executor 3   │ │
│ │ (Task 1)     │ │              │ │ (Task 2)     │ │              │ │ (Task 3)     │ │
│ └──────────────┘ │              │ └──────────────┘ │              │ └──────────────┘ │
└──────────────────┘              └──────────────────┘              └──────────────────┘
  1. Driver Node: The orchestrator. It executes the main program, creates the SparkSession, translates code into logical Directed Acyclic Graphs (DAGs), and schedules tasks across worker nodes.
  2. Executors: Worker processes running on cluster nodes. They execute individual data processing tasks concurrently and store cached data in RAM.
  3. Cluster Manager: Allocates cluster hardware resources across nodes.

3. Core Data Structures: RDDs vs. DataFrames

 

                             Spark Data Abstractions
                                        │
           ┌────────────────────────────┴────────────────────────────┐
           ▼                                                         ▼
[ Resilient Distributed Datasets (RDD) ]                    [ Spark DataFrames ]
• Low-level, unstructured data API                           • Higher-level, structured API
• Functional programming syntax                              • Tabular format (rows & columns)
• Manual optimization required                               • Automated Catalyst Optimizer tuning

RDDs (Resilient Distributed Datasets)

The foundational abstraction in Spark. An RDD is an immutable, fault-tolerant collection of elements partitioned across cluster nodes.
  • Resilient: Rebuilds lost partitions automatically using lineage graphs if a worker node crashes.
  • Distributed: Data is split into partitions across multiple machines.

DataFrames & Datasets

Introduced to provide a tabular abstraction similar to Pandas or relational database tables. Spark DataFrames are built on top of RDDs but benefit from the Catalyst Optimizer, which rewrites execution plans automatically for high performance.

4. Transformations, Actions, and Lazy Evaluation

Spark processes data using Lazy Evaluation: it does not compute transformations immediately when you write code. Instead, it records transformations as a Directed Acyclic Graph (DAG) and executes them only when an Action is explicitly called.
Python

from pyspark.sql import SparkSession
from pyspark.sql.functions import col

# Initialize a local PySpark Session
spark = SparkSession.builder \
    .appName("Spark101_Demo") \
    .getOrCreate()

# Load dataset (Lazy step)
df = spark.read.csv("data/large_transactions.csv", header=True, inferSchema=True)

# --- TRANSFORMATIONS (Lazy - Builds execution plan, runs nothing yet) ---
filtered_df = df.filter(col("amount") > 100)
grouped_df = filtered_df.groupBy("category").sum("amount")

# --- ACTION (Triggers actual execution across the cluster) ---
results = grouped_df.collect() # Triggers job execution
grouped_df.show(5)             # Triggers job execution

Transformations vs. Actions Summary

Category Examples Behavior
Transformations select(), filter(), groupBy(), join(), withColumn() Lazy evaluation; builds DAG lineage without modifying physical data.
Actions show(), count(), collect(), write.parquet(), take() Eager evaluation; triggers cluster computation and returns results.

5. PySpark Code Example: Distributed Data Processing

Below is a PySpark pipeline loading data, performing filtering and aggregation, and writing partitioned outputs to Parquet format:
Python

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, avg

# Create PySpark session
spark = SparkSession.builder.appName("DataScienceWorkflow").getOrCreate()

# Read structured data
df = spark.read.json("s3a://my-company-bucket/user_activity.json")

# Process using SQL-like syntax and vectorized expressions
processed_df = (
    df.where(col("status") == "ACTIVE")
      .groupBy("country")
      .agg(avg("session_duration").alias("avg_session_mins"))
      .orderBy(col("avg_session_mins").desc())
)

# Write output partitioned by country (Optimized for fast distributed reads)
processed_df.write \
    .mode("overwrite") \
    .partitionBy("country") \
    .parquet("data/output/country_analytics.parquet")

Key Takeaways

  1. Horizontal Scaling: Apache Spark enables data scientists to scale computation horizontally by splitting large datasets across clusters of worker nodes.
  2. In-Memory Speed: Keeps data in RAM between operations, avoiding costly disk reading and writing.
  3. Prefer DataFrames: Use Spark DataFrames over low-level RDDs whenever possible to take advantage of the built-in Catalyst Optimizer.
  4. Lazy Evaluation: Transformations construct a DAG plan; actual cluster computation is triggered only when an Action (show(), count(), write()) is executed.

About Adi Status

Adi Satus is a passionate financial writer with a keen interest in the ever-evolving world of loans, insurance, technology, and cryptocurrency. With years of experience researching and writing on a broad range of financial topics, Hindi Me Gyaan aims to simplify complex concepts and make them accessible for readers. Whether you're looking to secure a loan, navigate the world of insurance, explore the latest tech trends, or understand the intricacies of cryptocurrency, Hindi Me Gyaan provides expert insights and practical advice to help you make informed decisions. Always staying updated with the latest developments, Hindi Me Gyaan is dedicated to bringing you the most relevant, timely, and useful information to guide you on your financial journey.

View all posts by Adi Status →

Leave a Reply

Your email address will not be published. Required fields are marked *