SDP versus hand-written classic Spark jobs?
Summary
- A hand-written Spark job is a program: you read, transform and write in the order you choose, and you set up the run, retries and sequencing. In Spark Declarative Pipelines (SDP), added in Spark 4.1, you declare datasets and queries and Spark handles the execution graph, dependency ordering, parallelism, checkpoints and retries.
- Databricks frames the choice as procedural versus declarative processing: procedural gives full control over execution, declarative leaves execution to the system.
- SDP adds rules a classic job doesn't have. Dataset functions return a DataFrame and must not call methods such as
collect(),count(),saveAsTable()orstart(). - The two coexist. Lakeflow Jobs can run a pipeline task next to notebook and Python script tasks.
SDP versus hand-written classic Spark jobs?
A classic Spark job is a program. You read, transform and write in the order you choose, and you decide how it runs, retries and sequences against other jobs. Spark Declarative Pipelines (SDP) moves that work into the framework. You declare what each dataset contains, and the Spark 4.1.0 release notes say Spark "handles the execution graph, dependency ordering, parallelism, checkpoints, and retries."
Databricks frames the choice as procedural versus declarative processing. It says Apache Spark "primarily follows a procedural model", while Lakeflow Pipelines, built on SDP, automate "orchestration, compute management, monitoring, data quality enforcement, and error handling."
What does the same transformation look like both ways?
SDP in Python:
from pyspark import pipelines as dp
from pyspark.sql import DataFrame
@dp.materialized_view
def daily_orders_by_state() -> DataFrame:
return (
spark.table("customer_orders")
.groupBy("state", "order_date")
.count()
.withColumnRenamed("count", "order_count")
)
SDP in SQL:
CREATE MATERIALIZED VIEW daily_orders_by_state
AS SELECT state, order_date, count(*) order_count
FROM customer_orders
GROUP BY state, order_date;
A hand-written job:
daily = (
spark.table("customer_orders")
.groupBy("state", "order_date")
.count()
.withColumnRenamed("count", "order_count")
)
daily.write.mode("overwrite").saveAsTable("daily_orders_by_state")
The SDP definitions only describe the result. The hand-written job also picks the save mode and the target table, and something outside it has to run it after whatever builds customer_orders.
How do they differ?
| Dimension | SDP | Hand-written job |
|---|---|---|
| You write | Dataset definitions: @dp.materialized_view, @dp.table, CREATE MATERIALIZED VIEW |
Read, transform and write steps |
| Run order | The pipeline analyzes dependencies, then orders and parallelizes the work | You sequence the steps, and an orchestrator sequences jobs |
| Checkpoints | The pipeline spec's storage directory is where they can be stored |
The checkpointLocation option on each streaming write |
| Retries | Handled by the framework | A retry policy you configure, for example on a Lakeflow Jobs task |
| Writes in your code | Not allowed in dataset functions | Wherever you need them |
| Languages | Python and SQL | Python, Scala, Java, R and SQL |
| Check before running | spark-pipelines dry-run reports syntax, analysis and graph errors without reading or writing data |
Your own tests |
| Data quality rules | Expectations, which are part of Lakeflow Pipelines and not Apache Spark | Your own checks in code |
What rules does SDP add to your code?
SDP "evaluates the code that defines a pipeline multiple times during planning and pipeline runs", so a dataset function should contain only the code that defines the table or view. It must return a pyspark.sql.DataFrame. The guide says never to use collect(), count(), pivot(), toPandas(), save(), saveAsTable(), start() or toTable() in that code, and SDP SQL doesn't support the PIVOT clause. A job that depends on these calls needs a rewrite before it can become a pipeline, or it stays hand-written.
When should you use each?
The Databricks docs say to choose declarative processing when "Simplified development and maintenance are priorities." That fits several datasets that feed each other, where you want ordering, parallelism, checkpoints and retries handled for you. They say to choose procedural processing when "Fine-grained control over execution logic is required", when "Transformations involve complex business rules that are difficult to express declaratively", or when "Performance optimizations necessitate manual tuning."
How do they fit on Databricks?
Lakeflow Pipelines extend SDP and add features such as data quality expectations, a queryable event log and continuous mode. The Databricks docs say pipelines "retry transient failures progressively" from the Spark task to the flow to the whole pipeline. Lakeflow Jobs covers the procedural side: Databricks says to "Use Lakeflow Jobs to add explicit execution logic". A job's task types include notebook, Python script and pipeline, tasks can depend on each other, and a task can have a retry policy.
FAQs
Which Spark version added SDP?
Spark 4.1.0. Its release notes list SDP as a new component.
Can I write SDP pipelines in Scala?
The Spark guide documents Python and SQL, and pipeline source files are .py or .sql.
Can one job mix pipelines and hand-written steps?
Yes. In Lakeflow Jobs a pipeline task can sit beside notebook and Python script tasks.
Does SDP run on the same cluster managers as other Spark jobs?
spark-pipelines is built on spark-submit, so it supports every cluster manager that spark-submit supports.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.