• Data Engineering Home
  • Data Engineering Learning Path

Apache Spark Fundamentals

What is Apache Spark?

Apache Spark is an open-source distributed data processing engine designed for large-scale data analytics, ETL, machine learning, and real-time stream processing.

Instead of processing data on a single machine, Spark distributes work across multiple machines (nodes or workers) and processes data in parallel, making it significantly faster and more scalable than traditional approaches.

Key Benefits

Why Apache Spark was Created?

Traditional Hadoop MapReduce processed data where each stage required reading and writing back to disk, causing significant overhead.

Read -> Process -> Write
Read -> Process -> Write
Read -> Process -> Write

Whereas Spark introduced in-memory processing that dramatically improved performance for many data processing workloads.

Read -> Process in Memory -> Process in Memory -> Write

Apache Spark Architecture

Apache Spark architecture showing a Driver coordinating multiple Executors, with each Executor running inside a Worker Node.

Driver

The driver:

Executors

Executors:

Core Components of Apache Spark

Spark consists of several modules built on top of Spark Core:

Apache Spark core components diagram showing Spark SQL, Structured Streaming, MLlib, and GraphX built on top of Spark Core.

Spark Core

The foundation of Spark

Example: everything in Spark ultimately executes through Spark Core.

rdd = spark.sparkContext.parallelize([1,2,3,4]

Spark SQL

Used for processing structured and semi-structured data. It supports,

spark.sql("""
SELECT * FROM customers
""")

or

df = spark.read.table("customers")

In Databricks and modern Spark development, Spark SQL is the most frequently used component.

Structured Streaming

Provided real-time stream processing. Example pipeline:

Kafka -> Spark Stream -> Delta Table -> Dashboard

Common use cases:

MLlib

MLlib is Spark’s distributed machine learning library. It provides scalable implementations of common ML algorithms, feature extraction tools, and pipeline management.

from pyspark.ml.classification import LogisticRegression
from pyspark.ml.feature import VectorAssembler

# Combine features into a single vector column
assembler = VectorAssembler(inputCols=["age", "income"], outputCol="features")
feature_df = assembler.transform(df)

# Train a distributed logistic regression model
lr = LogisticRegression(featuresCol="features", labelCol="label")
model = lr.fit(feature_df)

GraphX (Graph Processing)

GraphX is Spark’s API for graph-parallel computation. It extends RDDs to support directed multigraphs with attributes attached to vertices and edges.

(Customer)-[:OWNS]->(Account)

Spark APIs & Programming Languages

Spark provides APIs for multiple programming languages.

Apache Spark provides APIs that allow developers to work with Spark using Scala, Java, Python, R, and SQL.

Scala API

Spark was originally developed in Scala.

val df = spark.read.table("customers")

Advantages:

Java API

It is used in enterprise Java applications.

Dataset<Row> df = spark.read().json("data.json");

Python API (PySpark)

It is most commonly used today. Popular in:

df = spark.read.table("customers")

R API (SparkR)

It is mainly used by statistical analytics.

df <- read.df("customers.csv")

SQL Interface

It is supported directly through Spark SQL.

SELECT * FROM customers

What is PySpark?

PySpark is the Python API for Apache Spark. It is NOT a separate processing engine. PySpark simply allows Python applications to control Spark.

A simple design to emphasize that PySpark is an API/interface, not a separate engine.

Is PySpark Included in Spark?

Yes, a standard Spark installation already contains PySpark.

spark/
├── bin/
├── jars/
└── python/
    └── pyspark/

Therefore, you can immediately use it without installing anything additional.

from pyspark.sql import SparkSession

Why Does pip install pyspark Exist?

This provides:

pip install pyspark

For Python developers who want Spark without manual installation.

How PySpark Works Internally

Spark itself runs on the JVM. PySpark communicates through a bridge.

PySpark architecture showing a Python application communicating through the Py4J bridge with the JVM, where the Apache Spark core engine runs.

PySpark provides the Python interface, while the actual Spark engine runs in the JVM. The Python process communicates with the JVM-based Spark application through a bridge such as Py4J.

Key Spark Concepts

Spark Session

The entry point to Spark.

from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()

Everything starts with spark object.

DataFrame

Distributed table-like structure.

df = spark.read.csv("customer.csv")
print(df)
id    name      city
1      John       Pune
2      Smith     Mumbai

Transformation (Lazy Evaluation)

Transformations are operations that construct a new DataFrame from an existing one without immediately evaluating the data (e.g., filter(), select(), groupBy(), join()).

Spark records these operations in a Lineage Graph (Directed Acyclic Graph or DAG). Execution is deferred until an action is called, allowing Spark’s Catalyst Optimizer to optimize the execution plan (e.g., predicate pushdown and column pruning).

df.filter(df.age > 18)
df.select("name")

Actions (Eager Execution)

Actions trigger the physical computation of the execution plan built by transformations. They compile the DAG into physical stages and tasks distributed across worker nodes, returning results to the Driver process or saving data to external storage.

# Triggers execution and displays top rows
df.show(5)

# Triggers execution to aggregate matching rows
total_count = df.count()

# Triggers physical write to storage
df.write.mode("overwrite").parquet("/mnt/data/output")

# Triggers physical write to storage
df.write.mode("overwrite").parquet("/mnt/data/output")

Understanding spark.read.table()

df = spark.read.table("customers")

Many developers think this is a Spark Core operation, but actually the primary component involved is Spark SQL.

Internal Flow

PySpark internal workflow showing how spark.read.table("customers") passes through the PySpark API and JVM to resolve the table, create a logical DataFrame plan, and later execute the optimized physical plan on Spark executors when an action is triggered.

Relationship Between Spark and Databricks

Databricks is a managed platform built around Spark. It provides: