MongoDB实时同步

本文提供MongoDB实时同步场景的实施思路、配置示例和验证方式,可作为实际项目配置参考。

实现在mongoDB中结果表向目标表中实时同步数据

案例

mongoDB中将cdc_source中的数据实时同步到cdc_target中

前提

  • 启用MongoDB副本集模式

解决方案

创建Flink SQL作业类型,添加以下依赖

20250109182905

CREATE TABLE from_table(
    _id STRING,
    name STRING,
    age INT,
    city STRING,
    PRIMARY KEY (_id) NOT ENFORCED
) WITH (
    'connector'='mongodb-cdc',
    'hosts'='localhost:27017',
    'username' = 'root',
    'password' = 'root123',
    'collection'='t_source',
    'database'='isxcode_db'
);

CREATE TABLE target_table(
    _id STRING,
    name STRING,
    age INT,
    city STRING,
    PRIMARY KEY (_id) NOT ENFORCED
) WITH (
    'connector'='mongodb',
    'uri'='mongodb://root:root123@localhost:27017',
    'collection'='t_sink',
    'database'='isxcode_db'
); 

INSERT INTO target_table select * from from_table;
配置项说明
connector连接方式
uri数据库连接url
collection表名
database库名