Debezium中如何将不同数据库表路由至不同Topic?
Debezium多表Topic路由配置方案
针对你的需求——默认表路由到user_service.$tableName,指定表路由到专属服务Topic,可通过配置多个ByLogicalTableRouter实例实现多表路由,完整配置如下:
"transforms": "unwrap,RerouteNotifications,RerouteAlerts,RerouteDefault", // 处理notifications表路由 "transforms.RerouteNotifications.type": "io.debezium.transforms.ByLogicalTableRouter", "transforms.RerouteNotifications.topic.regex": ".*\\.notifications", "transforms.RerouteNotifications.topic.replacement": "notification_service.notifications", // 处理alerts表路由 "transforms.RerouteAlerts.type": "io.debezium.transforms.ByLogicalTableRouter", "transforms.RerouteAlerts.topic.regex": ".*\\.alerts", "transforms.RerouteAlerts.topic.replacement": "alert_service.alerts", // 处理users和clients默认路由 "transforms.RerouteDefault.type": "io.debezium.transforms.ByLogicalTableRouter", "transforms.RerouteDefault.topic.regex": "(.*)\\.(users|clients)", "transforms.RerouteDefault.topic.replacement": "user_service.${2}"
配置说明
- 路由顺序优先级:先配置特殊表(notifications、alerts)的路由规则,再处理默认表(users、clients),避免匹配逻辑冲突。
- 正则匹配逻辑:
.*\\.表名适配Debezium默认生成的Topic格式(通常为数据库名.模式名.表名),确保精准定位对应表的事件流。 - 动态Topic生成:默认表的路由使用
${2}引用正则捕获到的表名,自动生成user_service.表名格式的目标Topic。 - 拼写修正:你原有配置中的
notificaton_service为拼写错误,已修正为notification_service。
内容的提问来源于stack exchange,提问作者JDev
相关产品推荐
相关产品推荐

