Ingest a database from CDC files
A change data capture (CDC) service reads the change log of a database and writes it to object storage as files. Each row is one insert, update, or delete, with the operation type and an ordering column. The files are appended over time and never modified. This page imports every change file into a managed log table, and builds a current-state table with one row per key with a pipeline.
Use it when your CDC tool can only write files, or when you want the raw change history in the lakehouse. If your CDC provider can write Iceberg tables into Bauplan directly and deduplicate for you, see how to stream CDC into Bauplan.
The examples on this page use this layout:
- main.pycreates the branch, imports the change files, runs the pipeline, merges
- bauplan_project.yaml
- schemas.pyOrders
- orders.sqlraw.orders_log -> app.orders
Write change files to a bucket
Configure the CDC service to write change files (Parquet, Avro or newline-delimited JSON) to a bucket Bauplan can read. Some CDC services write straight into a warehouse without producing files you can see. Switch them to write files to object storage.
Each change row must carry the primary key of the source row, the operation type, and a column that orders changes (a log sequence number or a source timestamp).
Import the change files into a log table
Every run works on its own data branch, created from main and deleted after the merge. On the first run, create the log table from the change files of one table:
from datetime import datetime, timezone
import bauplan
client = bauplan.Client()
stamp = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
branch = f"{client.info().user.username}.orders-cdc-prod-{stamp}"
client.create_branch(branch=branch, from_ref="main")
uri = "s3://my-bucket/cdc/app_db/orders/*.parquet"
if not client.has_table(table="orders_log", ref=branch, namespace="raw"):
client.create_table(table="orders_log", search_uri=uri, branch=branch, namespace="raw")
Then import on every run. Files already imported are automatically skipped by Bauplan, and each run adds only the change files written since the last one:
state = client.import_data(table="orders_log", search_uri=uri, branch=branch, namespace="raw")
if state.job_status != "SUCCESS":
raise RuntimeError(f"import failed: {state.error}")
CDC services often write many small files. See how to lay out data on object storage. A column that is null in a whole micro-batch can also be written with a null type, which conflicts with the typed files at table creation. See how to control types at import.
Rebuild current state with a pipeline
The log table holds every version of every row. For two orders, it can look like this:
order_id | customer_id | amount | op | change_seq | change_ts |
|---|---|---|---|---|---|
| 1 | 42 | 100.0 | insert | 1001 | 2026-11-20 09:00:00 |
| 2 | 7 | 35.0 | insert | 1002 | 2026-11-20 09:05:00 |
| 1 | 42 | 120.0 | update | 1003 | 2026-11-21 14:30:00 |
| 2 | 7 | 35.0 | delete | 1004 | 2026-11-22 08:15:00 |
To get the current state, keep the latest change for each key, and drop the key if that change is a delete. Here the current state is a single row: order 1 with an amount of 120.0. Order 2 was deleted.
Do this in a SQL model. Declare the output schema in a Python file at the top of the project:
import bauplan
class Orders(bauplan.TableSchema):
"""Current state of the orders table, one row per order."""
order_id: bauplan.Int64
customer_id: bauplan.Int64 | None
amount: bauplan.Float64 | None
updated_at: bauplan.TimestampMicroUTC
Then put the SQL in orders.sql:
-- bauplan: materialization_strategy = REPLACE
-- bauplan: output_schema = Orders
SELECT order_id, customer_id, amount, updated_at
FROM (
SELECT
order_id,
customer_id,
amount,
change_ts AS updated_at,
op,
ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY change_seq DESC) AS rn
FROM raw.orders_log
) AS latest
WHERE rn = 1 AND op != 'delete'
Run it on the same branch after the import, check the result, and merge:
run = client.run(project_dir="orders_state", ref=branch, namespace="app")
if run.job_status != "SUCCESS":
raise RuntimeError(f"run failed: {run.error}")
client.merge_branch(source_ref=branch, into_branch="main")
client.delete_branch(branch=branch)
Point downstream models at app.orders. The log table still holds old versions and deleted rows.
This rebuild reads the whole log on every run, and gets slower as the log grows. When it gets too slow, move deduplication to a CDC provider that writes into Bauplan directly.
If deduplication already runs outside Bauplan, you could skip the log table. Every run would then have to replace the whole table, because an import only appends and skips files it has already seen.