Sub topic 3
Streaming tables for incremental append-style data
This chapter assumes you know basic SQL, Delta Lake tables, Unity Catalog catalogs and schemas, and the difference between batch and streaming data. You should also have a pipeline with source files and a target schema, as covered in the previous chapter.
What a streaming table is
A streaming table is a Delta table that a pipeline updates by processing only new input. Each update reads the records that arrived since the last update, appends the results, and records its progress in a checkpoint. Records that were already processed are not read again.
This makes streaming tables a good fit for append-style data such as files landing in storage, event streams, and the bronze layer of a pipeline. The source must be append-only. If the source rewrites or deletes existing rows, the stream fails unless you configure it to handle changes.
Defining a streaming table in SQL
Use CREATE OR REFRESH STREAMING TABLE and read the source with the STREAM keyword. The query below ingests JSON files from a Unity Catalog volume. read_files uses Auto Loader, which later chapters cover in detail.
CREATE OR REFRESH STREAMING TABLE orders_bronze
AS SELECT *
FROM STREAM read_files(
'/Volumes/main/demo/raw/orders/',
format => 'json'
);The STREAM keyword tells the pipeline to read the source incrementally. Without it, the query is a batch read and would reprocess everything on each run.
Defining a streaming table in Python
In Python, a decorated function returns a streaming DataFrame, and the function name becomes the table name.
import dlt
@dlt.table(comment="Raw orders ingested with Auto Loader")
def orders_bronze():
return (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.load("/Volumes/main/demo/raw/orders/")
)The function must return a streaming DataFrame. A function that uses spark.read returns a batch DataFrame, and that defines a different kind of dataset.
Reading from another streaming table
A downstream table reads an upstream streaming table as a stream. In SQL, wrap the name in STREAM(...). In Python, use dlt.read_stream.
CREATE OR REFRESH STREAMING TABLE orders_silver
AS SELECT
order_id,
customer_id,
CAST(amount AS DECIMAL(10,2)) AS amount,
CAST(order_ts AS TIMESTAMP) AS order_ts
FROM STREAM(orders_bronze);The pipeline sees the reference and runs orders_bronze before orders_silver.
How updates behave
Each update processes only the new input and appends it. Three behaviors matter in practice:
- Rerunning an update with no new data adds no rows.
- Changing the query does not rewrite rows that already exist. Old rows keep the old logic and new rows get the new logic.
- A full refresh clears the table and its checkpoint, then reprocesses the source from the start.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Query is rejected or reads all data each run | Missing `STREAM` or a batch read in Python | Use `STREAM(...)` or `readStream` |
| Update fails after upstream rows change | Source is not append-only | Use a different pattern such as AUTO CDC, covered later |
| Old rows do not reflect a new transformation | Streaming tables do not reprocess history | Run a full refresh |
Exercise
You change the CAST in orders_silver from DECIMAL(10,2) to DECIMAL(12,2) and run a normal update. Do existing rows change? How would you apply the new logic to all rows?
Show answer
Existing rows do not change, because a streaming table processes only new input and appends it. Only rows that arrive after the change use the new logic. To apply it to all rows, run a full refresh of orders_silver, which clears the table and checkpoint and reprocesses the source. This works only if the upstream data is still available.