Skip to content

Auto CDC Flows

dp.create_auto_cdc_flow or CREATE FLOW AS AUTO CDC creates an Auto CDC flow into a target streaming table from a Change Data Feed source.

AutoCDC Target Streaming Table

Target table should be created using the dp.create_streaming_table function.

# Neither Delta Lake nor Iceberg work
# Used as formats for demo purposes only

from pyspark import pipelines as dp
from pyspark.sql import DataFrame


@dp.table
def cdc_events() -> DataFrame:
    return spark.readStream.format("delta").table("cdc_source")

dp.create_streaming_table(
    name="customers",
    format="iceberg",
)

dp.create_auto_cdc_flow(
    target="customers",
    source="cdc_events",
    keys=["id"],
    sequence_by="event_ts",
    apply_as_deletes="op = 'DELETE'",
    except_column_list=["op", "event_ts"],
    stored_as_scd_type=2,
    track_history_column_list=["address"],
)

Auto CDC flows are represented as AutoCdcFlows.

AutoCdcFlow is resolved to AutoCdcMergeFlow (when FlowResolver is requested to resolve a flow).

AutoCdcMergeFlow applies a CDC event stream to a target table using MERGE operation.

AutoCdcMergeFlow can be one of the two SCD Types (based on ChangeArgs):

  • SCD Type 1
  • SCD Type 2

AutoCdcMergeFlow SCD Type 1 (with a Table output) is executed as a Scd1MergeStreamingWrite. All the other variants fail at execution either with an UnsupportedOperationException or an AnalysisException.

Scd1MergeStreamingWrite is executed as a streaming query with foreachBatch (Spark Structured Streaming) streaming operator and Trigger.AvailableNow.

SupportsRowLevelOperations

Spark Declarative Pipelines asserts that the AutoCDC target streaming table supports row-level data mutations that require rewriting data.

Auto CDC flows (with MERGE writes) require a target table to use a Spark SQL connector with SupportsRowLevelOperations (Spark SQL) support.