Upsert-Kafka Connector
Overview
The Upsert-Kafka connector is a changelog variant of the Kafka connector, used to read and write DMS for Kafka topics in upsert mode. Unlike the regular Kafka connector, which only processes append data, the Upsert-Kafka connector interprets Kafka messages as changelog records, supporting insert, update, and delete operations by primary key. It is suitable for change data capture (CDC) scenarios that require end-to-end exactly-once semantics.
| Category | Details |
|---|---|
| Supported type | Source table and result table |
| Running mode | Stream mode |
| Data format | For details, see Formats. |
| API type | SQL, DataStream |
| Whether result table can be updated or deleted | Yes |
Application Scenarios
- CDC data pipeline: Writes change data captured by CDC tools such as Debezium to Kafka in upsert mode. When the downstream system consumes the data, it is automatically merged into the latest state based on the primary key.
- Real-time data warehouse synchronization: Writes retraction records (-U/+U) generated by Flink aggregation calculations to Kafka, allowing downstream consumers to obtain complete change semantics.
- Cross-system data synchronization: Serves as a change data transmission channel between different systems to ensure data consistency.
- Event sourcing: Persists domain events to Kafka in the form of change logs, supporting state reconstruction and time-travel queries.
- Dimension table broadcasting: Broadcasts change data of dimension tables to Kafka, and downstream jobs update the local dimension cache based on the primary key.
Notes and Constraints
- It is incompatible with the common Kafka connector (connector = 'kafka'). You are not advised to use both connectors to read and write the same topic.
- Topic must be created in advance for default data consumption.
Differences from the Common Kafka Connector
| Item | Kafka Connector | Upsert-Kafka Connector |
|---|---|---|
| Connector value | kafka | upsert-kafka |
| Primary key requirement | Not required | PRIMARY KEY must be defined. |
| Data semantics | Append-only stream | Change log stream (Changelog) |
| Source output | Original message | Latest status after deduplication by primary key |
| Sink write | Append-only write | Upsert write by primary key (update or insert) |
| Deletion support | Not supported | Sink supports deletion (the -D record is written as NULL). |
| Application scenario | Event stream and log collection | CDC synchronization, status update, and dimension broadcast |
| Message format | Common key-value pair | The key is the primary key field value, and the value is a complete record or NULL (deletion). |
Syntax Structure
CREATE TABLE {table_name} (
{column_name} {column_type},
...
PRIMARY KEY ({pk_column}) NOT ENFORCED,
WATERMARK FOR {time_column} AS {watermark_expression}
) WITH (
'connector' = 'upsert-kafka',
'topic' = '{kafka_topic}',
'properties.bootstrap.servers' = '{broker_list}',
'properties.group.id' = '{consumer_group}',
'key.format' = '{key_format}',
'value.format' = '{value_format}'
);
The Upsert-Kafka connector must specify the serialization formats for both the key and the value. The key corresponds to the primary key field, and the value corresponds to the non-primary key fields. When a record is deleted, the value is NULL.
WITH Parameters
Source parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| connector | Yes | String | None | upsert-kafka | Specifies that the connector type is Upsert-Kafka. This parameter is mandatory and must be set to the fixed value upsert-kafka. The Flink runtime loads the Upsert-Kafka source factory class based on this value, declares the table as a changelog data source, and deduplicates and merges messages by primary key. |
| topic | Yes | String | None | Valid topic name | Specifies the name of the Kafka topic to be consumed. This parameter is mandatory for the source. The Flink runtime allocates a subtask to each topic partition for parallel consumption. Only the latest value is retained for records with the same primary key. |
| properties.bootstrap.servers | Yes | String | None | Host:port list | Specifies the list of Kafka broker addresses. Use commas (,) to separate multiple addresses. This parameter is mandatory for both the source and sink. The Flink runtime uses this configuration to create a Kafka Consumer or Producer client connection. You are advised to configure multiple broker addresses to ensure high availability. |
| properties.group.id | No | String | None | Valid consumer group name | Specifies the ID of a Kafka consumer group. This parameter is mandatory for the source. Flink uses this group ID to identify the consumer during running. Consumers with the same group ID share the consumption offset of topic partitions. |
| key.format | Yes | String | None | json/csv/avro | Specifies the deserialization format of the primary key field. This parameter is mandatory for both the source and sink. Flink deserializes the key part (primary key field) of a Kafka message based on this format during running. The serialization and deserialization of the primary key field must use the same format as that used for writing. |
| value.format | Yes | String | None | json/csv/avro | Specifies the deserialization format of non-primary key fields. This parameter is mandatory for both the source and sink. The Flink runtime deserializes the value part (non-primary key fields) of Kafka messages based on this format. The value of a deleted record is NULL. |
| scan.topic-partition-discovery.interval | No | Duration | None | Positive integer (ms) | Specifies the check interval for dynamic partition discovery. After this parameter is set, new partitions in the topic can be automatically discovered. |
| properties.* | No | String | - | Kafka consumer configuration | Any configuration parameter passed to the Kafka consumer, prefixed with properties.. After removing the properties. prefix, the configuration item name is directly passed through to the Kafka consumer. |
Sink parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| connector | Yes | String | None | upsert-kafka | Specifies that the connector type is Upsert-Kafka. This parameter is mandatory and must be set to the fixed value upsert-kafka. When Flink is running, it loads the Upsert-Kafka Sink factory class based on the value of this parameter, declares the table as a changelog output table, and performs upsert write based on the primary key. |
| topic | Yes | String | None | Valid topic name | Specifies the name of the Kafka topic to which data is written. This parameter is mandatory for the sink. When Flink is running, it sends all output records to this topic. The key is the primary key field value, and the value is a complete record or NULL (indicating that the record is deleted). |
| properties.bootstrap.servers | Yes | String | None | Host:port list | Specifies the list of Kafka broker addresses. Use commas (,) to separate multiple addresses. This parameter is mandatory for both the source and sink. |
| key.format | Yes | String | None | json/csv/avro | Specifies the serialization format of the primary key field. This parameter is mandatory for the sink. The Flink runtime serializes the primary key field into the Key part of the Kafka message based on this format, which is used for partition routing and primary key identification. |
| value.format | Yes | String | None | json/csv/avro | Specifies the serialization format of the non-primary key field. This parameter is mandatory for the sink. The Flink runtime serializes non-primary key fields into the value part of Kafka messages based on this format. When a record is deleted, the value is serialized to NULL. |
| sink.buffer-flush.max-rows | No | Integer | None | Positive integer | Specifies the maximum number of rows in the sink buffer. When the buffer is full, batch writes are triggered. Increasing this value reduces the number of writes and improves throughput, but increases latency. This parameter is valid only for at-least-once and none semantics. |
| sink.buffer-flush.interval | No | Duration | None | Valid duration, for example, 10 seconds or 1 minute. | Specifies the interval for flushing the sink buffer. Batch writes are triggered at this interval. This parameter and sink.buffer-flush.max-rows together control the flush policy. The flush is triggered when either condition is met. This parameter is valid only for at-least-once and none semantics. |
| properties.* | No | String | - | Kafka producer configuration | Any configuration parameter passed to the Kafka producer, prefixed with properties.. After removing the properties. prefix, the configuration item name is directly passed through to the Kafka producer. |
Key and value format parameters
The Upsert-Kafka connector requires specifying the formats for both the key and the value. Each format has its own independent parameters, with the prefixes key. and value., respectively.
JSON format parameters (Using value as an example; replace the prefix with key. in the same way.)
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| value.json.fail-on-missing-field | No | Boolean | false | true/false | Specifies whether to report an error when a field defined in the DDL is missing in the JSON message. The default value is false. If set to true, an exception is thrown when a missing field is encountered. If set to false, the missing field is set to null and processing continues. |
| value.json.ignore-parse-errors | No | Boolean | false | true/false | Specifies whether to ignore JSON parsing errors. The default value is false. If set to true, the field is skipped instead of reporting an error when a parsing error occurs. This improves fault tolerance but may cause data loss. |
| value.json.timestamp-format.standard | No | String | SQL | SQL/ISO-8601 | Specifies the parsing format standard for the timestamp type. The default value is SQL. The SQL format is parsed as yyyy-MM-dd HH:mm:ss.S, and the ISO-8601 format is parsed as yyyy-MM-dd'T'HH:mm:ss.S. |
CSV format parameters (using value as an example)
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| value.csv.field-delimiter | No | String | , | Single character | Specifies the delimiter of CSV fields. The default value is a comma (,). |
| value.csv.ignore-parse-errors | No | Boolean | false | true/false | Specifies whether to ignore CSV parsing errors. The default value is false. |
Usage Example
- Example 1: Consume changelog data from the Upsert-Kafka topic, automatically deduplicate and merge the data based on the primary key, and output the data to the Print result table.
-- Create an Upsert-Kafka source table in JSON format. CREATE TABLE upsert_kafka_order_source ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, update_time TIMESTAMP(3), PRIMARY KEY (order_id) NOT ENFORCED ) WITH ( 'connector' = 'upsert-kafka', 'topic' = 'order-changes', 'properties.bootstrap.servers' = 'kafka-broker:9092', 'properties.group.id' = 'order-consumer', 'key.format' = 'json', 'value.format' = 'json' ); -- Create the Print result table. CREATE TABLE print_sink ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, update_time TIMESTAMP(3) ) WITH ( 'connector' = 'print' ); -- Submit the job. INSERT INTO print_sink SELECT order_id, user_id, amount, status, update_time FROM upsert_kafka_order_source;
- Example 2: The key is in CSV format and the value is in JSON format. This format is applicable to scenarios where there are a small number of primary key fields.
-- Create an Upsert-Kafka source table (Key=CSV, Value=JSON). CREATE TABLE upsert_kafka_mixed_source ( user_id BIGINT, user_name STRING, city STRING, member_level STRING, update_time TIMESTAMP(3), PRIMARY KEY (user_id) NOT ENFORCED ) WITH ( 'connector' = 'upsert-kafka', 'topic' = 'user-profile-changes', 'properties.bootstrap.servers' = 'kafka-broker:9092', 'properties.group.id' = 'profile-consumer', 'key.format' = 'csv', 'value.format' = 'json' ); -- Create the Print result table. CREATE TABLE print_sink ( user_id BIGINT, user_name STRING, city STRING, member_level STRING, update_time TIMESTAMP(3) ) WITH ( 'connector' = 'print' ); -- Submit the job. INSERT INTO print_sink SELECT user_id, user_name, city, member_level, update_time FROM upsert_kafka_mixed_source;
- Example 3: The Upsert-Kafka end-to-end CDC pipeline forwards change data from one topic to another.
-- Upstream CDC source table CREATE TABLE upsert_kafka_cdc_source ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, PRIMARY KEY (order_id) NOT ENFORCED ) WITH ( 'connector' = 'upsert-kafka', 'topic' = 'cdc-order-changes', 'properties.bootstrap.servers' = 'kafka-broker:9092', 'properties.group.id' = 'cdc-relay-consumer', 'key.format' = 'json', 'value.format' = 'json' ); -- Downstream forwarding target table CREATE TABLE upsert_kafka_cdc_target ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, PRIMARY KEY (order_id) NOT ENFORCED ) WITH ( 'connector' = 'upsert-kafka', 'topic' = 'cdc-order-relay', 'properties.bootstrap.servers' = 'kafka-broker:9092', 'key.format' = 'json', 'value.format' = 'json' ); -- Forward change logs. INSERT INTO upsert_kafka_cdc_target SELECT order_id, user_id, amount, status FROM upsert_kafka_cdc_source;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot