Lazy evaluation means Spark waits to run your code until it really needs to. This saves time and computer power.
When you want to build a data processing plan without running it immediately.
When you want to combine many data steps before actually doing the work.
When you want Spark to optimize your data tasks for faster results.
When you want to avoid wasting resources on unnecessary calculations.
Syntax
Apache Spark
val data = spark.read.csv("file.csv")
val filtered = data.filter("age > 30")
// No action here, just transformations
val count = filtered.count() // Action triggers execution
Transformations like filter are lazy and do not run immediately.
Actions like count start the actual data processing.
Examples
This example shows how transformations are lazy and only run when an action like count() is called.
Apache Spark
val rdd = spark.sparkContext.textFile("data.txt")
val words = rdd.flatMap(line => line.split(" "))
// No job runs yet
val wordCount = words.count() // Action triggers job
Here, filter is lazy. The show() action runs the query and displays results.
Apache Spark
val df = spark.read.json("people.json")
val adults = df.filter("age >= 18")
// No execution yet
adults.show() // Action triggers execution and shows data
Sample Program
This code creates a small table, filters people older than 30 (lazy step), then counts them (action). The count triggers Spark to run the filter and count.
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("LazyEvalExample").getOrCreate()
# Load data
data = spark.createDataFrame([
(1, "Alice", 29),
(2, "Bob", 35),
(3, "Cathy", 23)
], ["id", "name", "age"])
# Transformation (lazy)
adults = data.filter("age > 30")
# No computation yet# Action triggers computation
count = adults.count()
print(f"Number of adults older than 30: {count}")
spark.stop()
OutputSuccess
Important Notes
Lazy evaluation helps Spark optimize by combining steps before running.
Actions include count(), collect(), show(), and others.
Transformations include filter(), map(), select(), etc.
Summary
Spark waits to run your data steps until an action is called.
This saves time and resources by optimizing the work.
Remember: transformations are lazy, actions trigger execution.
Practice
(1/5)
1. What does lazy evaluation mean in Apache Spark?
easy
A. Spark runs all transformations immediately when defined
B. Spark delays running transformations until an action is called
C. Spark only runs actions if there are no errors
D. Spark saves all data permanently after each step
Solution
Step 1: Understand lazy evaluation concept
Lazy evaluation means Spark does not run transformations right away but waits.
Step 2: Identify when Spark runs computations
Spark runs the actual work only when an action like collect() or count() is called.
Final Answer:
Spark delays running transformations until an action is called -> Option B
Quick Check:
Lazy evaluation = Delay until action [OK]
Hint: Remember: transformations wait, actions run [OK]
Common Mistakes:
Thinking transformations run immediately
Confusing actions with transformations
Believing Spark saves data after each step
2. Which of the following is the correct way to trigger execution in Spark?
easy
A. df.count()
B. df.select("name")
C. df.filter("age > 30")
D. df.withColumn("new", df["age"] + 1)
Solution
Step 1: Identify transformations vs actions
filter(), select(), and withColumn() are transformations (lazy).
Step 2: Recognize actions that trigger execution
count() is an action that forces Spark to run all prior transformations.
Final Answer:
df.count() -> Option A
Quick Check:
Action = count() triggers execution [OK]
Hint: Actions like count() run Spark jobs [OK]
Common Mistakes:
Choosing transformations as actions
Confusing withColumn as action
Thinking select triggers execution
3. What will be the output of this Spark code snippet?
val data = spark.range(1, 5)
val filtered = data.filter(_ % 2 == 0)
filtered.show()