Debezium

Updated at:

Debezium is a Changelog Data Capture (CDC) tool that streams database changes from MySQL, PostgreSQL, Oracle, and Microsoft SQL Server to Apache Kafka in real time. Debezium defines a unified format schema for changelogs and supports serialization via JSON and Apache Avro. The Apache Kafka connector and Object Storage Service (OSS) support the Debezium format.

Realtime Compute for Apache Flink interprets Debezium JSON and Debezium Avro messages as INSERT, UPDATE, or DELETE messages in Flink SQL. It can also write INSERT, UPDATE, or DELETE messages back as Debezium JSON or Avro messages and forward them to downstream stores such as Kafka.

Use cases

  • Synchronizing incremental data from a database to another system

  • Log auditing

  • Real-time materialized views of databases

  • Temporal joins of database tables

How it works

Important

Flink SQL cannot process a single UPDATE message that contains both UPDATE_BEFORE and UPDATE_AFTER data. When writing to a downstream system, Flink converts UPDATE_BEFORE data to DELETE and UPDATE_AFTER data to INSERT, then writes these as separate Debezium messages.

Example

The following example shows a Debezium JSON record for an update to the products table in MySQL. The record captures a weight change for the product with id=111.

{
  "before": {
    "id": 111,
    "name": "scooter",
    "description": "Big 2-wheel scooter",
    "weight": 5.18
  },
  "after": {
    "id": 111,
    "name": "scooter",
    "description": "Big 2-wheel scooter",
    "weight": 5.15
  },
  "source": {...},
  "op": "u",
  "ts_ms": 1589362330904,
  "transaction": null
}
Note

For field descriptions, see the Debezium documentation.

If this message is published to a Kafka topic named products_binlog, define a Flink table to read from that topic and interpret the change events:

-- Use the debezium-json format to interpret Debezium JSON messages.
CREATE TABLE topic_products (
  -- Make sure that the schema of the topic_products table is the same as the schema of the products table in the MySQL database.
  id BIGINT,
  name STRING,
  description STRING,
  weight DECIMAL(10, 2)
) WITH (
  'connector' = 'kafka',
  'topic' = 'products_binlog',
  'properties.bootstrap.servers' = 'localhost:9092',
  'properties.group.id' = 'testGroup',
  'format' = 'debezium-json'
  -- If the Debezium message is an Avro message, use the debezium-avro-confluent format.
);

If you configure Debezium Kafka Connect with value.converter.schemas.enable=true, the schema is embedded in the message body:

{
  "schema": {...},
  "payload": {
    "before": {
      "id": 111,
      "name": "scooter",
      "description": "Big 2-wheel scooter",
      "weight": 5.18
    },
    "after": {
      "id": 111,
      "name": "scooter",
      "description": "Big 2-wheel scooter",
      "weight": 5.15
    },
    "source": {...},
    "op": "u",
    "ts_ms": 1589362330904,
    "transaction": null
  }
}

To parse this format, add 'debezium-json.schema-include' = 'true' to the WITH clause. The default is false. Avoid including the schema in Debezium JSON messages — it increases message size and reduces parsing performance.

After registering the topic as a Flink table, use it as a changelog source:

-- Calculate the real-time average weight per product.
SELECT name, AVG(weight) FROM topic_products GROUP BY name;

-- Sync all data and incremental changes to an Elasticsearch index.
INSERT INTO elasticsearch_products
SELECT * FROM topic_products;

Format options

Flink provides two formats for Debezium messages: debezium-json for JSON messages and debezium-avro-confluent for Avro messages.

debezium-avro-confluent

Option

Required

Default

Type

Description

format

Yes

(none)

STRING

Set to debezium-avro-confluent to interpret Debezium Avro messages.

debezium-avro-confluent.basic-auth.credentials-source

No

(none)

STRING

The source of basic authentication credentials for Schema Registry.

debezium-avro-confluent.basic-auth.user-info

No

(none)

STRING

The basic authentication user information for Schema Registry.

debezium-avro-confluent.bearer-auth.credentials-source

No

(none)

STRING

The source of bearer authentication credentials for Schema Registry.

debezium-avro-confluent.bearer-auth.token

No

(none)

STRING

The bearer authentication token for Schema Registry.

debezium-avro-confluent.properties

No

(none)

MAP

Additional property mappings forwarded to Schema Registry, for options not exposed as Flink configuration options. Flink configuration options take priority over this setting.

debezium-avro-confluent.ssl.keystore.location

No

(none)

STRING

The location of the SSL keystore.

debezium-avro-confluent.ssl.keystore.password

No

(none)

STRING

The password of the SSL keystore.

debezium-avro-confluent.ssl.truststore.location

No

(none)

STRING

The location of the SSL truststore.

debezium-avro-confluent.ssl.truststore.password

No

(none)

STRING

The password of the SSL truststore.

debezium-avro-confluent.subject

No

(none)

STRING

The Confluent Schema Registry subject under which the schema is registered during serialization. The Apache Kafka and Upsert Kafka connectors default to <topic_name>-value or <topic_name>-key. Required when using the file system connector as a sink.

debezium-avro-confluent.url

Yes

(none)

STRING

The URL of Confluent Schema Registry for obtaining or registering schemas.

debezium-json

Option

Required

Default

Type

Description

format

Yes

(none)

STRING

Set to debezium-json to interpret Debezium JSON messages.

debezium-json.schema-include

No

false

BOOLEAN

Whether the Debezium JSON message includes schema information. Set value.converter.schemas.enable=true in Debezium Kafka Connect to embed the schema in the message body. Valid values: true (schema included) and false (schema not included).

debezium-json.ignore-parse-errors

No

false

BOOLEAN

How to handle parse errors. true: skip the field or row. false: return an error and fail the deployment.

debezium-json.timestamp-format.standard

No

SQL

STRING

The format for input and output timestamps. SQL: parses yyyy-MM-dd HH:mm:ss.s{precision} (for example, 2020-12-30 12:13:14.123). ISO-8601: parses yyyy-MM-ddTHH:mm:ss.s{precision} (for example, 2020-12-30T12:13:14.123).

debezium-json.map-null-key.mode

No

FAIL

STRING

How to handle null key values in a MAP. FAIL: return an error. DROP: discard rows with null keys. LITERAL: replace the null key with the string specified by debezium-json.map-null-key.literal.

debezium-json.map-null-key.literal

No

null

STRING

The string constant used to replace null key values when debezium-json.map-null-key.mode is set to LITERAL.

debezium-json.encode.decimal-as-plain-number

No

false

BOOLEAN

How to encode DECIMAL values. true: plain decimal notation (for example, 0.000000027). false: scientific notation (for example, 2.7E-8).

Data type mappings

Debezium uses the JSON format for serialization and deserialization. For data type mappings, see the JSON Format documentation and Confluent Avro Format documentation.

Available metadata

The following metadata fields can be declared as read-only (VIRTUAL) columns in DDL statements.

Important

Metadata fields are available only when the connector forwards format metadata. Only the Apache Kafka connector supports declaring metadata fields for its value format.

Key

Data type

Description

schema

STRING NULL

A JSON string describing the payload schema. Null if the schema is not included in the Debezium record.

ingestion-timestamp

TIMESTAMP_LTZ(3) NULL

The timestamp at which the connector processed the event. Corresponds to the ts_ms field in the Debezium record.

source.timestamp

TIMESTAMP_LTZ(3) NULL

The timestamp at which the source system created the event. Corresponds to the source.ts_ts field in the Debezium record.

source.database

STRING NULL

The source database. Corresponds to the source.db field in the Debezium record, if available.

source.schema

STRING NULL

The schema of the source database. Corresponds to the source.schema field in the Debezium record, if available.

source.table

STRING NULL

The source table. Corresponds to the source.table or source.collection field in the Debezium record, if available.

source.properties

MAP<STRING, STRING> NULL

A mapping of source properties. Corresponds to the source field in the Debezium record.

The following example shows how to declare metadata columns in a Kafka table using the Debezium JSON format:

CREATE TABLE KafkaTable (
  origin_ts TIMESTAMP(3) METADATA FROM 'value.ingestion-timestamp' VIRTUAL,
  event_time TIMESTAMP(3) METADATA FROM 'value.source.timestamp' VIRTUAL,
  origin_database STRING METADATA FROM 'value.source.database' VIRTUAL,
  origin_schema STRING METADATA FROM 'value.source.schema' VIRTUAL,
  origin_table STRING METADATA FROM 'value.source.table' VIRTUAL,
  origin_properties MAP<STRING, STRING> METADATA FROM 'value.source.properties' VIRTUAL,
  user_id BIGINT,
  item_id BIGINT,
  behavior STRING
) WITH (
  'connector' = 'kafka',
  'topic' = 'user_behavior',
  'properties.bootstrap.servers' = 'localhost:9092',
  'properties.group.id' = 'testGroup',
  'scan.startup.mode' = 'earliest-offset',
  'value.format' = 'debezium-json'
);

Caveats

Duplicate change events after a failure

Under normal conditions, Debezium delivers each change event exactly-once. If a failure occurs, Debezium falls back to at-least-once delivery and may send duplicate events to Kafka. This can produce incorrect results or unexpected exceptions in Flink queries.

To prevent this, set the deployment parameter table.exec.source.cdc-events-duplicate to true and define a primary key on the source table. Flink then generates an additional stateful operator that deduplicates change events and produces a normalized changelog stream.

Note

For details on Debezium's message delivery semantics, see the Debezium FAQ.

Data from the Debezium connector for PostgreSQL is not interpreted correctly

When using the Debezium connector for PostgreSQL to capture changes to Kafka, set REPLICA IDENTITY to FULL on the monitored table. The default is DEFAULT, which causes Flink SQL to misinterpret Debezium data.

With REPLICA IDENTITY FULL, UPDATE and DELETE events include the previous values of all columns. Without it, the before field contains only the primary key value — or null if no primary key is defined.

To change the setting, run:

ALTER TABLE <your-table-name> REPLICA IDENTITY FULL;