Sub topic 10
Directory listing versus file notification discovery
This chapter assumes you can write a basic readStream and writeStream pipeline and know what a checkpoint is. It covers the two ways Auto Loader discovers new files, and how to choose between them.
What file discovery means
Before Auto Loader can read a file, it has to learn that the file exists. This step is called file discovery. Auto Loader records every discovered file in the checkpoint, so a file is processed once. Discovery only decides how new files are found.
There are two modes:
- Directory listing, the default. Auto Loader lists the input path to find files it has not seen.
- File notification. Auto Loader sets up cloud services that send an event when a file arrives, and it reads those events.
Directory listing
Directory listing needs no extra setup. It works with the code you have already seen:
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))Auto Loader does not rescan everything naively on each run. It uses optimizations such as lexical ordering of file names, which are available in some storage layouts. Even so, each batch still makes list calls against storage. When a directory holds millions of files, those calls take longer and cost more.
Directory listing is a good fit when:
- The input directory is small or moderate in size.
- Files arrive in batches, for example hourly or daily.
- You run with
availableNow, so the listing cost is paid once per run. - You cannot create queues or event subscriptions in the cloud account.
File notification
File notification mode avoids listing. You turn it on with one option:
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"))When the stream starts, Auto Loader creates a notification service and a queue in your cloud account. On AWS this uses SNS and SQS. Each new file produces an event, and the stream reads the queue instead of listing the path.
File notification is a good fit when:
- The directory holds a very large number of files.
- You need low latency with a continuous stream.
- Files arrive at a high and steady rate.
The cost is setup. The cluster or the credentials it uses need permission to create and manage these cloud resources. Without them, the stream fails at startup.
Choosing a mode
| Question | Directory listing | File notification |
|---|---|---|
| Extra setup | None | Queue and event service |
| Extra permissions | None | Create and read queues and events |
| Large directories | Slower as listing grows | Scales better |
| Typical use | Scheduled or modest volume | High volume or low latency |
Start with directory listing. Switch only when listing time becomes a problem you can measure.
Common errors
- The stream fails with a permissions error right after you set
cloudFiles.useNotificationstotrue. The identity running the stream cannot create the queue or subscription. - Ingestion slows down over time with directory listing. The input folder keeps growing, so each list call returns more. Archive old files or move to file notification.
- You expect events to cover files that already existed. Notification mode reads events for new files, so older files need a separate backfill. Plan for this when you switch modes.
Exercise
Your team runs Auto Loader once an hour with availableNow on a folder that gets a few hundred files per hour. The cloud admins will not grant queue permissions. Which discovery mode do you use, and why?
Show answer
Directory listing. The volume is modest, the job runs on a schedule so listing cost is paid once per run, and it needs no queue or event permissions. File notification would add setup you cannot get and bring little benefit.