Sub topic 7
Spark as the engine underneath
This chapter assumes you know basic SQL and Python, and that you have seen the compute options (clusters, SQL warehouses, serverless) from the previous chapter. It explains what Apache Spark does when you run a query or a notebook cell on Databricks.
What Spark is
Apache Spark is an open source engine that processes large datasets by splitting the work across many machines. Databricks compute runs Spark. When you start a cluster, one machine acts as the driver and the others act as workers. The driver plans the work. The workers, through units called executors, do it in parallel.
You rarely manage this directly. You write SQL or PySpark, and Spark decides how to split and run it.
One engine, several languages
SQL, Python (PySpark), Scala, and R all use the same engine. A SQL query and the equivalent DataFrame code produce the same plan. A DataFrame is a table-like object with named columns, spread across the cluster.
In a Databricks notebook, a spark session is already defined for you. You do not create it.
from pyspark.sql import functions as F
df = spark.table("main.tutorial.orders")
totals = (
df.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
)
display(totals)Lazy evaluation
Most DataFrame operations are lazy. Calls such as groupBy and agg only describe a plan. Nothing runs until you request a result with an action such as display, count, or a write.
This lets Spark's optimizer look at the whole plan first. It can skip unneeded columns, push filters down to the storage layer, and choose an efficient join strategy.
How a query runs
Spark divides data into partitions. Each task works on one partition, so more executors means more partitions processed at once. A shuffle is a step where data moves between executors, for example to group rows by key. Shuffles are the most expensive part of many jobs.
Common errors
- Collecting too much data. Calling
.collect()or.toPandas()on a large DataFrame pulls everything onto the driver and can cause out-of-memory errors. Filter or aggregate first. - Looping over rows in Python. Row-by-row Python code gives up parallelism. Use built-in column functions instead.
- Assuming code ran. Because evaluation is lazy, a mistake in a transformation may only appear when an action runs.
- Oversized shuffles. A wide join or groupBy on a skewed key can leave one task doing most of the work.
Where Spark sits in the platform
Spark reads and writes Delta tables, which the next chapters cover. The engine processes the data, and Delta Lake provides the reliable table format underneath it. SQL warehouses use a Spark-compatible engine as well, so the same SQL runs on either kind of compute.
Exercise
A notebook cell runs df2 = df.filter("amount > 20") and finishes instantly on a very large table. Why, and what makes Spark do the actual work?
Show answer
filter is a lazy transformation, so Spark only records it in the plan. The work happens when an action such as display(df2), df2.count(), or a write is called.
Exercise
Why is calling .toPandas() on a multi-billion-row DataFrame risky?
Show answer
It collects all rows onto the driver, which has limited memory, so it can fail with an out-of-memory error. Filter or aggregate in Spark first.