Debezium
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
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
}
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 |
|
|
Yes |
(none) |
STRING |
Set to |
|
|
No |
(none) |
STRING |
The source of basic authentication credentials for Schema Registry. |
|
|
No |
(none) |
STRING |
The basic authentication user information for Schema Registry. |
|
|
No |
(none) |
STRING |
The source of bearer authentication credentials for Schema Registry. |
|
|
No |
(none) |
STRING |
The bearer authentication token for Schema Registry. |
|
|
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. |
|
|
No |
(none) |
STRING |
The location of the SSL keystore. |
|
|
No |
(none) |
STRING |
The password of the SSL keystore. |
|
|
No |
(none) |
STRING |
The location of the SSL truststore. |
|
|
No |
(none) |
STRING |
The password of the SSL truststore. |
|
|
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 |
|
|
Yes |
(none) |
STRING |
The URL of Confluent Schema Registry for obtaining or registering schemas. |
debezium-json
|
Option |
Required |
Default |
Type |
Description |
|
|
Yes |
(none) |
STRING |
Set to |
|
|
No |
false |
BOOLEAN |
Whether the Debezium JSON message includes schema information. Set |
|
|
No |
false |
BOOLEAN |
How to handle parse errors. |
|
|
No |
SQL |
STRING |
The format for input and output timestamps. |
|
|
No |
FAIL |
STRING |
How to handle null key values in a MAP. |
|
|
No |
null |
STRING |
The string constant used to replace null key values when |
|
|
No |
false |
BOOLEAN |
How to encode DECIMAL values. |
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.
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 |
|
|
STRING NULL |
A JSON string describing the payload schema. Null if the schema is not included in the Debezium record. |
|
|
TIMESTAMP_LTZ(3) NULL |
The timestamp at which the connector processed the event. Corresponds to the |
|
|
TIMESTAMP_LTZ(3) NULL |
The timestamp at which the source system created the event. Corresponds to the |
|
|
STRING NULL |
The source database. Corresponds to the |
|
|
STRING NULL |
The schema of the source database. Corresponds to the |
|
|
STRING NULL |
The source table. Corresponds to the |
|
|
MAP<STRING, STRING> NULL |
A mapping of source properties. Corresponds to the |
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.
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;
For details, see the Debezium connector for PostgreSQL documentation.