Schema Subsystem Architecture (pydbzengine.schema)¶
The pydbzengine.schema package compiles schema-enabled Debezium ChangeEvent batches into Apache Arrow (pyarrow) schemas, tables, and typed Python domain entities.
Downstream consumers that extend SchemaAwareChangeHandler receive compiled TableSchema descriptors, structural SchemaDiff objects, homogeneous TableBatch runs, and ordered lifecycle events rather than raw JSON strings or Java Kafka Connect SourceRecord objects.
Installation & Schema-Enabled Requirement
pydbzengine.schema requires the optional Arrow extra (pip install pydbzengine[arrow]) and schema-enabled events: either EventFormat.CONNECT (format="connect") or EventFormat.JSON (format="json") with schemas_enabled=True (converter.schemas.enable=true, the default). Passing schemaless JSON (schemas_enabled=False) fails fast with a ValueError. If you consume schemaless JSON payloads, subclass BasePythonChangeHandler directly.
Raw vs. Schema-Aware Consumption¶
| Capability | BasePythonChangeHandler |
SchemaAwareChangeHandler (pydbzengine.schema) |
|---|---|---|
| Input Contract | Raw ChangeEvent strings or Java SourceRecord proxies |
Typed TableBatch and ChangeRecord entities |
| Supported Modes | Connect, schema-enabled JSON, and schemaless JSON | Schema-enabled Connect and JSON (schemas_enabled=True) |
| Wire Format Resolution | Handled manually by the consumer | Resolved once from DebeziumConfig via configure() |
| Type Conversion | Raw JSON primitives, Base64 strings, or Java objects | Single-pass conversion to Python types and pyarrow.DataType |
| Tabular Output | Manual dictionary construction | Direct batch.to_arrow(target_schema=...) (pyarrow.Table) |
| Schema Tracking | Manual tracking per destination | Automatic on_table_created and on_schema_evolved hooks with SchemaDiff |
| Intra-Batch DDL & Lifecycle | Mixed schema versions and event kinds in one list | Homogeneous TableBatch runs plus composable of_kind(), group_by_kind(), and group_by_destination() sub-batches |
Public Surface¶
Import consumer-facing types from pydbzengine.schema:
| Category | Public Exports (from pydbzengine.schema import ...) |
|---|---|
| Domain Models | TableSchema, SchemaDiff, ChangeRecord, TableBatch, ChangeBatch, EventKind |
| Converters & Formats | EventConverter, ConnectEventConverter, JsonEventConverter, EventFormat |
| Consumer Base Class | SchemaAwareChangeHandler |
End-to-End Conversion Pipeline¶
flowchart TD
subgraph Engine["Debezium Embedded Engine (JVM / JPype)"]
DB[("Source Database")] -->|"CDC Stream"| DE["DebeziumEngine"]
end
DE -->|"list[ChangeEvent]"| HB["SchemaAwareChangeHandler.handle_batch()"]
subgraph ConverterLayer["EventConverter Layer"]
HB --> EC["EventConverter.convert(records)"]
EC -->|"EventFormat.CONNECT"| CEC["ConnectEventConverter\n(Java SourceRecord + Connect Schema)"]
EC -->|"EventFormat.JSON\nschemas_enabled=True"| JEC["JsonEventConverter\n(JSON {schema, payload} Envelope)"]
end
CEC & JEC -->|"Single-Pass Conversion\n+ Cached TableSchema"| CB["ChangeBatch\n(tuple[ChangeRecord, ...])"]
subgraph Dispatch["Batch Sub-Grouping & Schema Tracking"]
CB -->|"batch.to_table_batches()"| TB["Homogeneous TableBatch Runs\n(per destination & schema)"]
TB -->|"reconcile_schema()"| CHECK{"Tracked Schema\nfor Destination?"}
CHECK -->|"First Time Seen"| OTC["on_table_created(new_schema)"]
CHECK -->|"new_schema != old_schema"| OSE["on_schema_evolved(new_schema, old_schema, diff)"]
CHECK -->|"Unchanged"| HTB["handle_table_batch(table_batch)"]
OTC --> HTB
OSE --> HTB
CB -.->|"Optional in handle_change_batch():\nof_kind() / group_by_kind()"| LIFE["Custom Batch Handling\n(TOMBSTONE / SCHEMA_CHANGE / HEARTBEAT)"]
end
HTB --> SINK[("Target Sink\n(Snowflake, Iceberg, Parquet, DuckDB)")]
Deterministic Converter Resolution¶
At initialization, DebeziumEngine normalizes connector properties and format into a DebeziumConfig instance and configures the attached handler via handler.configure(config=self.config). SchemaAwareChangeHandler delegates to EventConverter.from_config(config=config) to instantiate the matching converter deterministically:
| Converter Class | Resolution Condition | Execution Behavior |
|---|---|---|
ConnectEventConverter |
format="connect" (EventFormat.CONNECT) |
Consumes in-memory Java Kafka Connect SourceRecord objects. Compiles valueSchema() and keySchema() once per destination and converts records in a single pass via convert() / convert_event(). |
JsonEventConverter |
format="json" and schemas_enabled=True (default) |
Consumes JSON records containing explicit {"schema": ..., "payload": ...} envelopes. Compiles embedded schemas once per destination and converts payloads in a single pass via convert() / convert_event(). |
If format="json" is configured with schemas_enabled=False (or converter.schemas.enable=false), EventConverter.from_config() raises ValueError immediately rather than falling back to schema inference.
Default Debezium Envelopes vs. Flat Rows (ExtractNewRecordState)¶
pydbzengine.schema is strictly pass-through with respect to payload shape: TableSchema.arrow_schema compiles the exact top-level struct emitted by Debezium, and ChangeRecord.value holds the converted dictionary matching that schema.
- Default Debezium Envelope (no SMT):
Debezium emits records with top-level
before,after,source,op,ts_ms, andtransactionfields.beforeandafterare compiled as nested Arrowstructcolumns. BothSchemaDiff(is_additive,is_compatible) andTableBatch.to_arrow(target_schema=...)understand nested struct evolution when source table columns are added or widened insidebefore/after. - Flat Table Rows (
ExtractNewRecordStateSMT): When you want flat table columns directly inschema.arrow_schemaandbatch.to_arrow()(for example, when loading into a relational warehouse or lakehouse table), configure Debezium'sio.debezium.transforms.ExtractNewRecordStatetransform withadd.fields(such asop) anddelete.tombstone.handling.mode(rewriteordrop). EachChangeRecord.valueis then a flat row dictionary with the table columns and any configured metadata fields at top level.
Schema Compilation & Caching¶
When ConnectEventConverter or JsonEventConverter encounters a destination for the first time—or observes an updated wire schema—it compiles the schema in a single pass:
flowchart LR
WIRE["Wire Schema\n(Java Connect Schema or JSON dict)"] --> COMP["Schema Compilation\n& Arrow Type Mapping"]
COMP --> DEC["Compiled pa.Schema +\nValue & Key Decoders"]
DEC --> TS["TableSchema\n(Cached per Destination)"]
- Schema Inspection: Reads each field's Connect wire type, Debezium logical type name, nullability, doc, and parameters.
- Arrow Mapping & Decoder Binding: Maps primitive, temporal, decimal, and nested container types to
pyarrow.Fielddescriptors (preservingdocanddebezium.logical_typein Arrow field metadata) and compiles format-specific value decoders. - Destination Caching: Builds an immutable
TableSchema(destination,arrow_schema,key_fields,key_schema) cached per destination alongside its decoders. Subsequent events with an equal wire schema reuse the cached entry without recompiling, and tombstones never evict active table schemas.
Next Steps¶
- Domain Models & Types: Reference for
TableSchema,SchemaDiff,ChangeRecord,TableBatch,ChangeBatch, and the Debezium-to-Arrow type mappings. - Schema-Aware Consumer & Examples: Lifecycle hooks on
SchemaAwareChangeHandlerand end-to-end consumer examples.