Sub topic 16
When to use declarative pipelines instead
This chapter assumes you can write a Structured Streaming query with Auto Loader, a checkpoint and a trigger. It also assumes you have deployed one as a Databricks job. Here you will decide when a hand-written query is the right tool and when a declarative pipeline is a better fit.
What a declarative pipeline is
A declarative pipeline (Lakeflow Spark Declarative Pipelines, formerly Delta Live Tables) is a managed way to define tables. You declare what each table should contain. Databricks works out the order of execution, manages checkpoints, handles retries and scales the cluster.
A streaming table is the pipeline's version of a streaming query that appends to a Delta table. It reads from a streaming source and processes each new record once.
from pyspark import pipelines as dp
from pyspark.sql import functions as F
@dp.table(name="orders_bronze")
def orders_bronze():
return (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.load("/Volumes/main/demo/landing/orders")
)
@dp.table(name="orders_5min")
def orders_5min():
return (
spark.readStream.table("orders_bronze")
.withWatermark("event_time", "10 minutes")
.groupBy(F.window("event_time", "5 minutes"), "customer_id")
.agg(F.count("*").alias("orders"))
)This code runs only inside a pipeline, not in a plain notebook cell. Compare it with the hand-written version. There is no checkpointLocation, no schemaLocation, no trigger and no toTable. The pipeline supplies those for you.
What the pipeline manages for you
- Checkpoints and schema locations. The pipeline creates and tracks them.
- Dependencies. The pipeline reads
orders_bronzeinsideorders_5minand runs the tables in the right order. - Retries and recovery. A failed update is retried without custom job logic.
- Data quality rules. Expectations let you drop or flag bad rows, or fail the update.
- Compute. The pipeline starts and stops its cluster, and can autoscale it.
Decision criteria
| Situation | Better choice |
|---|---|
| Bronze, silver and gold tables that depend on each other | Declarative pipeline |
| You want built-in data quality rules | Declarative pipeline |
| You want less operational code to maintain | Declarative pipeline |
| Writing to a non-Delta sink such as Kafka | Hand-written query |
| Custom `foreachBatch` logic beyond a standard upsert | Hand-written query |
| Full control of checkpoint paths, triggers and cluster settings | Hand-written query |
| One small stream with no downstream tables | Either, usually a hand-written job |
Trade-offs
A pipeline gives up control. You cannot choose the checkpoint location, and the tables are owned by the pipeline. Other jobs should not write to them. You also work within the pipeline runtime, so you debug through pipeline events and the pipeline UI instead of calling query.lastProgress yourself.
Migrating
Start with a pipeline for new multi-step ingestion. Moving an existing query is harder because its checkpoint does not carry over. Plan a cutover point and test it on a copy of the data first.
Exercise
Your team has a Kafka-to-Delta query. A second query reads that table, aggregates it and writes to a third table. Both are stateless apart from a watermark. A third requirement is to publish the final results to a Kafka topic. Which parts suit a declarative pipeline, and which do not?
Show answer
The Kafka-to-Delta ingestion and the aggregation form a chain of dependent Delta tables, so they suit a pipeline, which manages their checkpoints and ordering. Publishing to a Kafka topic is a non-Delta sink, so it fits a hand-written query, which reads the final table and writes to Kafka.