Sub topic 4
Running continuously or as an availableNow job
This chapter assumes you can write a Spark Structured Streaming query with readStream and writeStream, and that you know what a checkpoint is. It covers how the trigger decides when an Auto Loader stream runs and when it stops.
What a trigger controls
A trigger tells a streaming query when to start the next micro-batch. It does not change what the query reads. The cloudFiles source and the checkpoint behave the same under every trigger. Only the schedule and the stopping behavior differ.
Auto Loader supports two common patterns:
- Continuous (default trigger): the query starts a new micro-batch as soon as the previous one finishes. It keeps running until you stop it.
- availableNow: the query processes all files that are pending when it starts, then stops by itself.
Running continuously
If you set no trigger, the stream runs without stopping. When no new files exist, it waits and checks again. This gives low latency, but the cluster must stay up all the time.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.toTable("main.bronze.orders"))Use this when data must be available within seconds or minutes of arriving.
Running with availableNow
With availableNow=True, the query reads every file that has arrived since the last run, splits the work into micro-batches, and then stops. You can schedule it as a job, for example hourly or daily. The cluster only runs while there is work to do.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))Each run picks up where the last one ended, because the checkpoint records which files were already processed. If a run finds no new files, it finishes quickly without writing anything.
Choosing between them
| Question | Continuous | availableNow |
|---|---|---|
| Cluster lifetime | Always on | Only during the run |
| Latency | Seconds to minutes | As long as the schedule interval |
| Cost | Higher, steady | Lower, tied to the schedule |
| Typical use | Near-real-time dashboards | Hourly or daily loads |
Common errors
- Forgetting to wait.
toTablewithavailableNowreturns a query object right away. In a notebook, later cells can run before the data is loaded. Call.awaitTermination()on the returned query when later steps depend on the data. - Changing the checkpoint path. A new checkpoint location makes the stream forget its history. It will treat every file as new and load duplicates.
- Using a continuous stream for rare data. If files arrive once a day, an always-on cluster mostly sits idle. Switch to
availableNowon a schedule.
Exercise
A team receives one batch of files each night and wants the lowest cost. Which trigger should they use, and why does rerunning the job not reload old files?
Show answer
Use trigger(availableNow=True) in a job scheduled after the files arrive. The cluster stops when the pending files are processed. Rerunning does not reload old files because the checkpoint records which files were already ingested.