Debezium发布历史93

原文地址: https://debezium.io/blog/2020/09/15/debezium-auto-create-topics/

欢迎关注留言,我是收集整理小能手,工具翻译,仅供参考,笔芯笔芯.

自动创建 Debezium 更改数据主题

2020 年 9 月 15 日 作者: René Kerner

kafka 主题 生产 新闻 讨论

创建新主题/管道

当您使用分布式 Kafka Connect 时,您可能已经意识到,一旦启动 Kafka Connect,就已经为您创建了一些与 Kafka Connect 相关的内部主题:

kafka-topics.sh --bootstrap-server HOSTNAME:9092 --list

connect_configs

connect_offsets

connect_statuses

Kafka Connect 会使用适合这些内部主题需求的合理、自定义的默认主题配置自动为您完成此操作。

当您启动 Debezium 连接器时,Kafka 代理将根据代理中的默认配置(可能是自定义配置)创建捕获事件的主题(如果auto.create.topics.enable = true在代理配置中启用):

auto.create.topics.enable = true

default.replication.factor = 1

num.partitions = 1

compression.type = producer

log.cleanup.policy = delete

log.retention.ms = 604800000 ## 7 days

但通常,当您在生产环境中使用 Debezium 和 Kafka 时,您可能会选择使用 禁用 Kafka 的主题自动创建功能auto.create.topics.enable = false,或者您希望连接器主题的配置与默认值不同。在这种情况下,您必须预先为 Debezium 捕获的数据源创建主题。

但有好消息!从 Kafka Connect 版本 2.6.0 开始,这可以自动化,因为实现了KIP-158以支持使用 Kafka Connect 创建可定制的主题。

卡夫卡连接

自 Kafka 2.6.0 起,Kafka Connect 就启用了主题创建:

topic.creation.enable = true

如果您不想允许连接器自动创建主题,您可以false在 Kafka Connect 配置中将此值设置为(connect-distributed.properties文件或在使用Debezium 的 Kafka Connect 容器映像时通过环境变量CONNECT_TOPIC_CREATION_ENABLE)。

更新连接器配置

Kafka Connect 主题创建适用于组。default当没有定义与主题匹配的其他组时,总会使用一个组。

每个组都可以指定主题配置属性的集合,以及配置应应用的主题名称的正则表达式列表。

您可以指定所有主题级别配置参数来自定义如何创建组的匹配主题。

让我们看看如何扩展此 Postgres 配置以创建 Kafka Connect 主题:

{

"name": "inventory-connector",

"config": {

"connector.class": "io.debezium.connector.postgresql.PostgresConnector",

"tasks.max": 1,

"database.hostname": "postgres",

"database.port": 5432,

"database.user": "postgres",

"database.password": "postgres",

"database.dbname" : "postgres",

"database.server.name": "dbserver1",

"schema.include.list": "inventory"

}

}

默认配置

所有与其他组不匹配的主题都topic.creation将应用default组配置。

默认情况下replication.factor = 3,我们希望partitions = 10主题应该使用 key 进行压缩cleanup.policy = "compact",并且所有消息都应该使用 LZ4 压缩在硬盘上compression.type = "lz4"。

所以我们配置默认组:

{

"name": "inventory-connector",

"config": {

"connector.class": "io.debezium.connector.postgresql.PostgresConnector",

"tasks.max": 1,

"database.hostname": "postgres",

"database.port": 5432,

"database.user": "postgres",

"database.password": "postgres",

"database.dbname" : "postgres",

"database.server.name": "dbserver1",

"schema.include.list": "inventory",

复制代码
    "topic.creation.default.replication.factor": 3,
    "topic.creation.default.partitions": 10,
    "topic.creation.default.cleanup.policy": "compact",
    "topic.creation.default.compression.type": "lz4"
}

}

产品日志配置

在数据库inventory模式中,存在product以表名开头的表。

默认情况下,完全限定的表名称将被捕获到与 Debezium 具有相同名称的主题,例如模式products中的表将被捕获到主题。inventorydbserver1dbserver1.inventory.products

我们希望所有转到表名以 开头的主题的消息product都存储在保留时间为 3 个月/90 天的主题中,并带有cleanup.policy": "delete"和retention.ms = 7776000000、replication.factor = 1、partitions = 20,并且仅使用生产者使用的压缩格式compression.type": "producer"。

您可以省略与集群默认值匹配的属性,但要小心,一旦更改 Kafka 代理上的默认配置,生成的主题配置可能会有所不同!

首先我们需要productlog使用该topic.creation.groups属性注册一个组。

然后我们可以定义该组中应包含哪些主题名称,并指定组的配置,就像我们对default组所做的那样:

{

"name": "inventory-connector",

"config": {

"connector.class": "io.debezium.connector.postgresql.PostgresConnector",

"tasks.max": 1,

"database.hostname": "postgres",

"database.port": 5432,

"database.user": "postgres",

"database.password": "postgres",

"database.dbname" : "postgres",

"database.server.name": "dbserver1",

"schema.include.list": "inventory",

"topic.creation.default.replication.factor": 3,

"topic.creation.default.partitions": 10,

"topic.creation.default.cleanup.policy": "compact",

"topic.creation.default.compression.type": "lz4",

复制代码
    "topic.creation.groups": "productlog",  

    "topic.creation.productlog.include": "dbserver1\\.inventory\\.product.*",  
    "topic.creation.productlog.replication.factor": 1,
    "topic.creation.productlog.partitions": 20,
    "topic.creation.productlog.cleanup.policy": "delete",
    "topic.creation.productlog.retention.ms": 7776000000,
    "topic.creation.productlog.compression.type": "producer"
}

}

表 1. 用于自定义自动主题创建的连接器配置

物品 描述

1

topic.creation.groups定义附加组名称的逗号分隔列表。这里我们只定义我们的productlog组。

2

该topic.creation.productlog.include字段包含以逗号分隔的正则表达式列表,这些正则表达式与productlog应应用组配置的主题名称相匹配。该productlog组匹配所有以 开头的主题dbserver1.inventory.product。

探索结果

当我们现在启动连接器并使用它kafka-topics.sh来查看主题是如何创建的时,我们可以看到所有内容都按定义工作:

the dbserver1.inventory.products topic has the config from the productlog group:

kafka-topics.sh --bootstrap-server HOSTNAME:9092 --describe --topic dbserver1.inventory.products

Topic: dbserver1.inventory.products PartitionCount: 20 ReplicationFactor: 1

Configs: compression.type=producer,cleanup.policy=delete,retention.ms=7776000000,segment.bytes=1073741824

the dbserver1.inventory.orders topic has the config from the default group:

kafka-topics.sh --bootstrap-server HOSTNAME:9092 --describe --topic dbserver1.inventory.orders

Topic: dbserver1.inventory.orders PartitionCount: 10 ReplicationFactor: 3

Configs: compression.type=lz4,cleanup.policy=compact,segment.bytes=1073741824,delete.retention.ms=2592000000

结论

在许多环境中,特别是在生产环境中,我们通常不希望在 Kafka 代理端启用主题自动创建,或者我们需要与默认主题配置不同的配置。

在 Kafka 2.6 之前,这只有在预先手动创建主题或通过某些自定义设置过程(可能在部署期间)时才可能实现。

自 Kafka 2.6 起,Kafka Connect 就为连接器主题提供了内置主题创建功能,本文介绍了如何将其与 Debezium 一起使用。

您可以在 GitHub 上的 Debezium 示例存储库中找到示例。

相关推荐
二哈喇子!2 小时前
MySQL数据更新操作
数据库·sql
二哈喇子!2 小时前
MySQL命令行导入数据库
数据库·sql·mysql·vs code
心动啊1212 小时前
SQLAlchemy 的使用
数据库
二哈喇子!4 小时前
基于SpringBoot框架的网上购书系统的设计与实现
java·大数据·spring boot
Justice link4 小时前
K8S基本配置
运维·docker·容器
观熵4 小时前
SaaS 系统的自动化部署结构设计实战指南:基于 K8s + Helm 的工程落地路径
运维·kubernetes·自动化·saas 架构
chinesegf4 小时前
ubuntu中虚拟环境的简单创建和管理
linux·运维·ubuntu
若涵的理解4 小时前
一文读懂K8S kubectl 命令,运维小白必看!
运维·docker·kubernetes
曾经的三心草4 小时前
redis-2-数据结构内部编码-单线程-String命令
数据结构·数据库·redis
java_logo4 小时前
2025 年 11 月最新 Docker 镜像源加速列表与使用指南
linux·运维·docker·容器·运维开发·kylin