Sub topic 13
Deploying a Streaming Query as a Job
This chapter assumes you can write a streaming query with a checkpoint and a trigger, and that you know how to use Unity Catalog volumes and tables. Here you move a query from a notebook into a Databricks job that runs without you.
Choose how the job runs
The trigger you picked earlier decides the job's shape. There are two common patterns.
| Pattern | Trigger | Job type | Cost profile |
|---|---|---|---|
| Continuous | `processingTime` or default | Continuous job, runs all the time | Cluster stays up |
| Scheduled incremental | `availableNow=True` | Triggered job on a schedule | Cluster runs only during the batch |
Use the scheduled pattern when minutes of latency are acceptable. The cluster starts, drains new data, and stops. Use the continuous pattern when you need low latency.
Write the job as a script
A job runs a notebook or a Python file. Keep the query in one place, give it a name, and wait for it to finish.
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
df = (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/demo/chk/orders_schema")
.load("/Volumes/main/demo/landing/orders")
)
query = (
df.writeStream
.queryName("orders_bronze_ingest")
.option("checkpointLocation", "/Volumes/main/demo/chk/orders")
.trigger(availableNow=True)
.toTable("main.demo.orders_bronze")
)
query.awaitTermination()awaitTermination() blocks until the query ends. Without it, a script can reach its last line and exit while the query is still running. The task then reports success or stops early, depending on how it was started. With availableNow, the call returns when all available data is processed. With a continuous trigger, it blocks until the query fails or you cancel it.
Configure the job
In the Jobs UI, create a job with one task that points to your notebook or file.
- Choose a job cluster, not an all-purpose cluster. A job cluster is created for the run and ends with it, and it costs less.
- For a scheduled pattern, add a schedule or a file-arrival trigger on the landing path.
- For a continuous pattern, choose the continuous trigger type. Databricks restarts the job if a run fails.
- Set retries on the task so a transient failure restarts the query.
- Add failure notifications to an email address or a webhook.
Restarts and recovery
A restart is safe because the checkpoint records which data was processed. The query resumes from the last committed batch. Retries and scheduled runs both depend on this, which is why the checkpoint must live on durable storage such as a Unity Catalog volume and not on the cluster's local disk.
Some changes need a new checkpoint. Changing the stateful logic, such as the grouping keys or the aggregation, usually breaks recovery from the old state. Plan these changes as a deliberate migration.
Common errors
- Script exits early. The code is missing
awaitTermination(). - Data reprocessed after a deploy. The checkpoint path changed.
- Two runs at once. Two concurrent runs sharing one checkpoint conflict. Set the job's maximum concurrent runs to 1.
- Schema errors on a new column. Auto Loader stops on a schema change by default, and a retry then picks up the evolved schema.
Exercise
A job runs hourly with trigger(availableNow=True). After a code change, a teammate points the query at a new checkpoint path, and the job reprocesses the whole landing folder. Why, and how do you prevent it?
Show answer
The checkpoint holds the record of processed files. A new path has no record, so the query starts from the beginning of the source. Keep the checkpoint path fixed, for example in a job parameter, and review changes to it as you would a schema change.