Why Spark: driver, executors, lazy evaluation
Spark / PySpark · 12 questions
Short notes
Key idea
Spark processes data too big for one machine by splitting it into partitions and working on them in parallel across a cluster. A driver plans the work; executors run tasks. Nothing runs until you ask for a result: Spark is lazy.
Syntax
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("sales").getOrCreate()
df = spark.read.parquet("gs://raw/orders/")
big = df.filter(df.amount > 1000) # nothing runs yet
big.count() # action: the job runs now
- Job (per action) → stages (split at shuffles) → tasks (one per partition).
- The Spark UI shows jobs, stages and where time goes.
Example
Reading 500 files, filtering and counting: Spark builds a plan, pushes the filter down to the file read, and runs one task per partition on all executors at once.
Traps
- Spark is overkill for small data. Under a few GB, pandas or SQL is faster and simpler.
collect()pulls everything to the driver and can crash it; useshow(),take()or write out.- Laziness hides where errors are: a bad transformation fails only when an action runs.
- Python UDFs are slow; prefer built-in functions.
Want the full lesson with step-by-step examples, a project and a certificate? Spark / PySpark course, ₹120 →
- Question 1easy
Which Spark component runs the tasks that process the data?
- Question 2easy
When does Spark actually process data?
- Question 3medium
orderswas created withspark.read.parquet(...). At which line does Spark start reading order rows from storage and filtering them?big = orders.filter(F.col("amount") > 1000) # line 1 ids = big.select("order_id") # line 2 n = ids.count() # line 3 - Question 4medium
Why is
df.collect()on a 200 GB DataFrame a bad idea? - Question 5medium
You need to clean a 2 GB CSV once a day. What's the simplest good choice?
Why Spark: driver, executors, lazy evaluation
Why Spark: driver, executors, lazy evaluation
Why Spark: driver, executors, lazy evaluation
Why Spark: driver, executors, lazy evaluation
Why Spark: driver, executors, lazy evaluation
7 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.