Sub topic 12
Setting up file notification mode
This chapter assumes you can already run a basic cloudFiles stream with a schema location and checkpoint, and that you know the difference between directory listing and file notification discovery. Here you will turn notifications on and handle what goes wrong.
What changes in notification mode
In directory listing mode, Auto Loader lists the input path on each batch to find new files. In file notification mode, Auto Loader asks your cloud provider to publish an event whenever a file lands. Auto Loader then reads those events from a queue instead of listing the directory.
| Cloud | Event service | Queue |
|---|---|---|
| AWS | S3 event notifications, SNS | SQS |
| Azure | Event Grid | Queue Storage |
| Google Cloud | Pub/Sub notifications | Pub/Sub subscription |
Turning it on
Set cloudFiles.useNotifications to true. Everything else about the stream stays the same.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.useNotifications", "true")
.option("cloudFiles.maxFilesPerTrigger", "500")
.load("s3://my-bucket/landing/events"))On the first start, Auto Loader creates the event service and queue for you, if the credentials it runs with are allowed to create them. If you cannot grant those permissions, an administrator can create the resources ahead of time and you can pass their details through the provider-specific cloudFiles options. Check the documentation for your cloud for the exact option names.
Rules to keep in mind
- The resources belong to the stream. Auto Loader records them in the checkpoint. If you delete the checkpoint and start over, the old queue stays behind and keeps costing money. Delete the old resources first, or clean them up with the cleanup tooling in the Databricks documentation.
- Permissions come first. Most first-run failures are permission errors from creating the topic, queue, or subscription. Read the stack trace for the denied action and grant exactly that.
- Events can be missed. Providers do not guarantee that every event arrives. Set
cloudFiles.backfillInterval, for example1 day, so Auto Loader periodically does a listing to catch files whose events were lost. - Overlapping paths conflict. Some providers allow only one notification configuration per bucket prefix. Two streams on overlapping prefixes may fail or interfere with each other.
- Existing files are not announced. Files already present before setup have no events. Auto Loader still lists them for the initial backfill, so you do not lose them.
Common errors
- A stream starts but ingests nothing. The queue exists, but the bucket is not publishing events to it. Check that the notification configuration points at the right prefix.
- The stream fails at startup with an authorization error. The cluster or job identity lacks permission to create or read the queue.
- Costs rise after you delete streams. Orphaned queues and topics remain. List and remove them.
Exercise
A stream in notification mode has run for weeks. You delete its checkpoint folder to reprocess everything, then restart it. What problem should you expect, and how do you avoid it?
Show answer
Auto Loader created the queue and event resources the first time and stored their details in the checkpoint. With the checkpoint gone, it creates new resources and leaves the old ones running, which is wasted cost and possible conflicts on the bucket. Delete the old notification resources before removing the checkpoint.