Sub topic 11
Applying change data feeds with AUTO CDC
This chapter assumes you know how streaming tables work in a pipeline, can read the bronze and silver pattern, and understand primary keys. It also assumes basic Python, since the main example uses dlt.apply_changes.
Why change data capture needs special handling
A change data capture (CDC) feed is a stream of row-level changes from a source system. Each record says that a row was inserted, updated or deleted. If you append the feed to a table, you get a history of events. You do not get the current state of each entity.
You could write a MERGE INTO statement to apply the changes. That is hard to get right when events arrive late or out of order. AUTO CDC handles this for you. You name the key columns and an ordering column. The pipeline then keeps the target table in sync with the feed.
The apply_changes call
The example below keeps the latest row per customer.
import dlt
dlt.create_streaming_table("customers_silver")
dlt.apply_changes(
target="customers_silver",
source="customers_cdc_feed",
keys=["customer_id"],
sequence_by="updated_at",
stored_as_scd_type=1,
)The call has three parts:
create_streaming_tabledeclares the target. The target must be a streaming table that you declare yourself. AUTO CDC does not define it for you.sourceis the dataset that carries the change feed. It is often a bronze streaming table.apply_changesis the flow that reads the source and writes to the target.
Newer Databricks releases also expose this as create_auto_cdc_flow. The arguments are the same in the cases shown here. Check your runtime's documentation for the name it supports.
The key arguments
| Argument | Purpose |
|---|---|
| `keys` | Columns that identify a row. Changes with the same key refer to the same entity. |
| `sequence_by` | Column that orders events, such as a timestamp or a version number. |
| `stored_as_scd_type` | `1` keeps only the latest row. `2` keeps a history of changes. |
The sequence_by column is what makes out-of-order data safe. If an older update arrives after a newer one, the pipeline compares the sequence values and keeps the newer row. Pick a column that increases reliably for each key. Ingestion time is a poor choice, because it reflects when you received the event, not when it happened.
SCD type 1 and type 2
SCD stands for slowly changing dimension. It describes how a table treats changes to an attribute over time.
- Type 1 overwrites the old values. The target always shows the current state. Past values are lost.
- Type 2 keeps each version of a row. The pipeline adds
__START_ATand__END_ATcolumns that show when each version was valid. The current version has a null end value.
Choose type 1 for a simple current-state table. Choose type 2 when you must answer questions such as "what was this customer's address last March?"
Handling deletes
A feed often marks deletions with an operation column. You can tell the flow to treat matching rows as deletes with the apply_as_deletes argument. For example, apply_as_deletes=expr("operation = 'DELETE'") removes those rows from the target. Import expr from pyspark.sql.functions.
Common errors
- Missing sequence column. Without a trustworthy
sequence_by, the pipeline cannot resolve conflicts. Make sure the column exists and is not null. - Duplicate sequence values for one key. Two events with the same key and the same sequence value are ambiguous. Use a column that breaks ties, or a struct of several columns.
- Reading the target as a plain append table. With SCD type 2, a key appears in several rows. Filter on
__END_AT IS NULLto get current rows. - Using a materialized view as the source. The source must be a stream of changes. Use a streaming table or another streaming source.
Exercise
A CDC feed delivers two updates for customer 42. The update with updatedat = 10:05 arrives first. The update with updatedat = 10:01 arrives later. With keys=["customerid"], sequenceby="updated_at" and SCD type 1, which row does the target hold, and why?
Show answer
The row from 10:05. The pipeline compares the sequence_by values, not the arrival order. The 10:01 event is older than the row already applied, so it does not overwrite it.
Exercise
Your business needs to report a customer's address as it was on a past date. Which storedasscd_type do you choose, and which columns help you find the right row?
Show answer
Choose type 2. Use the _STARTAT and _ENDAT columns. The row where the date falls between them holds the address that was valid at that time.