Skip to main content

Stream CDC into Bauplan

Some CDC providers write Iceberg tables directly into Bauplan, through its Iceberg REST catalog. They read the change log of a database and keep the current state of each table on a data branch. You do not import files or deduplicate rows yourself.

You pay the provider for this, and for the compute it uses to write the tables. To deduplicate in Bauplan instead, see how to ingest a database from CDC files.

Create a service user

Create a dedicated Bauplan user with write access for ingestion, and give its credentials to the provider. The branches the provider writes to are then owned by that user.

Create the ingestion branch and namespace

The provider writes to one data branch per source. Never point it at main, which would publish every change it commits before any audit runs. See how to bring data to production.

Create the branch and the namespace with the service user's credentials. The namespace must exist on the branch before the provider writes:

Branch and namespace
bauplan branch create <service_user>.app_db-ingestion-prod --from-ref main
bauplan namespace create app_db --branch <service_user>.app_db-ingestion-prod

Point the provider at the branch

Configure the provider with the Iceberg REST catalog URL of the ingestion branch, the namespace and the service user's credentials:

SettingValue
Catalog URLhttps://api.use1.aprod.bauplanlabs.com/iceberg/<branch>
Warehousedefault
Namespaceapp_db
Base locationYour Bauplan warehouse location, for example s3://<bucket>/iceberg

Replace <branch> with the ingestion branch, for example <service_user>.app_db-ingestion-prod. The branch in the catalog URL makes the provider write to your ingestion branch and not to main. Check that every table the provider writes lands in the app_db namespace. Wait until the tables appear on the ingestion branch before writing audits against them.

The provider writes the data files with its own compute, and needs write access to the bucket.

Write frequency and backfills drive the provider's compute cost. A full backfill of a large source rewrites every table. Sync each source only as often as its data needs to be fresh.

Exclude deleted rows

The tables hold the current state of each source table, deduplicated by the provider on the key. Providers also add metadata columns, such as the last operation on each row. Many keep rows deleted at the source and flag them in that column instead of removing them. In that case every read must exclude them. Replace op and 'd' below with the column and value your provider uses, and double-quote column names that contain characters such as /.

models.py
from typing import Annotated

import bauplan
import pyarrow as pa


class LiveOrders(bauplan.TableSchema):
"""Orders that still exist in the source database."""

id: bauplan.Int64
customer_id: bauplan.Int64 | None
amount: bauplan.Float64 | None


@bauplan.model(materialization_strategy="REPLACE")
@bauplan.python("3.13")
def live_orders(
orders: Annotated[
pa.Table,
# Deleted rows stay in the provider's table, flagged in the operation column
bauplan.Model("app_db.orders", projection_schema=LiveOrders, filter="op != 'd'"),
],
) -> Annotated[pa.Table, LiveOrders]:
return orders

Audit and publish on a schedule

The provider keeps committing to the ingestion branch, while a scheduled job publishes what the provider wrote:

  1. Read the head commit of the ingestion branch, and work only on that commit.
  2. Run an audit on it. The audit is a standard Bauplan project made only of expectations, run as a dry run.
  3. If the audit passes, copy the tables that changed into a short-lived branch from main, and merge that branch.

The job uses this layout:

    • publish.pyreads the ingestion commit, runs the audit, publishes
        • bauplan_project.yaml
        • expectations.pyexpectations on the provider's tables

Audit the latest commit of the ingestion branch

The provider commits to the ingestion branch continuously. To audit and publish a consistent state, read the head commit of the branch at the start of the run, audit that commit, and publish only what you audited:

publish.py
from datetime import datetime, timezone

import bauplan

client = bauplan.Client()
ingestion_commit = client.get_branch("<service_user>.app_db-ingestion-prod").hash

audit = client.run(
project_dir="audits/app_db",
ref=ingestion_commit,
namespace="app_db",
dry_run=True,
cache=False,
)
if audit.job_status != "SUCCESS":
raise RuntimeError(f"audit failed: {audit.error}")

The audit project contains expectations and the SQL models they read. It runs as a dry run and writes nothing. See how to Write-Audit-Publish.

Publish through a short-lived branch

Do not merge the ingestion branch into main. It lives for months, and merging a long-lived branch is slow and conflict-prone. Copy the audited tables into a short-lived branch from main and merge that instead:

publish.py
main_commit = client.get_branch("main").hash

# A table changed if its metadata file differs between the two commits
on_main = {t.name: t.metadata_location for t in client.get_tables(ref=main_commit, filter_by_namespace="app_db")}
changed_tables = [
t.name
for t in client.get_tables(ref=ingestion_commit, filter_by_namespace="app_db")
if on_main.get(t.name) != t.metadata_location
]

stamp = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
publish = f"{client.info().user.username}.app_db-publish-prod-{stamp}"
client.create_branch(branch=publish, from_ref=main_commit)
client.create_namespace(namespace="app_db", branch=publish, if_not_exists=True)
try:
for table in changed_tables:
client.revert_table(table=table, namespace="app_db", source_ref=ingestion_commit, into_branch=publish, replace=True)
client.merge_branch(source_ref=publish, into_branch="main")
finally:
client.delete_branch(branch=publish, if_exists=True)

Comparing metadata files finds the changed tables without reading any data. See how to handle merge conflicts for why.