Learn Apache Spark by Doing
What is Lazy Evaluation?
Lazy evaluation means delaying the execution of transformations until an action is explicitly called.
When you apply transformations (e.g.,
filter,select,map), Spark does not immediately process the data.Instead, it builds a logical execution plan describing the steps needed.
Only when an action (e.g.,
show(),count(),collect(),write()) is called does Spark:Compile the logical plan into an optimized physical plan.
Distribute the work efficiently across the cluster.
Why Lazy Evaluation?
This design gives Spark several advantages:
Global Optimization
Spark can analyze the full sequence of transformations before execution.
This allows it to combine, rearrange, or eliminate unnecessary operations.
Efficiency at Scale
For example, predicate pushdown ensures filters (
WHEREclauses) are applied as early as possible, reducing the amount of data read.
Fault Tolerance
Because Spark remembers the transformation plan (not just results), it can recompute missing data after a failure.
Lazy Evaluation in Action
# Step 1: Read CSV (transformation, no execution yet)
df = spark.read.csv("users.csv", header=True)
# Step 2: Apply filter (still a transformation, Spark just records it)
filtered = df.filter(df['user_id'] > 1)
# Step 3: Action – triggers execution
filtered.show()
👉 Notes:
Steps 1 & 2 don’t actually read or filter the data yet.
Only at Step 3 (
show()= action), Spark executes the plan:Reads the CSV.
Applies the filter.
Displays the results.
Summary
Spark uses lazy evaluation to defer execution until an action is called.
This allows Spark to optimize the full DAG (Directed Acyclic Graph) of transformations.
Actions trigger execution, while transformations just define the plan.
Lazy evaluation is a key reason why Spark is both efficient and scalable.
Finish Course Early?
You have not completed all required lessons and assessments.