如何配置 Debezium 以使用特定列作为 Kafka 消息键?

How to configure Debezium to use specific column as Kafka message key?

默认情况下,Debezium 使用 table 的主键作为消息键。例如,如果您有 table

create table users
(
    id            bigint auto_increment primary key,
    department_id bigint
);

有数据

+----+----------------+
| id | department_id  |
+----+----------------+
|  5 |              1 |
|  6 |              1 |
|  7 |              2 |
+----+----------------+

Debezium 将生成以下 Kafka 消息:

Key: {"id": 5} Value: {"id": 5, "department_id": 1}
Key: {"id": 6} Value: {"id": 6, "department_id": 1}
Key: {"id": 7} Value: {"id": 7, "department_id": 2}

问题是如何配置 Debezium 以使用 department_id 或任何其他列作为 Kafka 消息密钥?

这里有 message.key.columns 参数。在您的连接器配置中,您应该这样设置:

{
  "name": "my-connector",
  "config": {
    "connector.class": "io.debezium.connector.mysql.MySqlConnector",
    "tasks.max": "1",
    "database.hostname": "mysql",
    "database.port": "3306",
    "database.whitelist": "my_database",
    ...
    "message.key.columns": "my_database.users:department_id"
  }
}

所有关系型 Debezium 连接器都支持此参数。

您可以在此处找到更多信息:

https://debezium.io/blog/2019/09/26/debezium-0-10-0-cr2-released/ https://debezium.io/documentation/reference/1.0/assemblies/cdc-mysql-connector/as_deploy-the-mysql-connector.html#mysql-connector-configuration-properties_debezium