Data_web(九)mongodb增量同步到mongodb

本文详细讲解了如何通过JSON配置连接MongoDB,包括设置MongoDB Reader和Writer的参数,如速度限制、查询条件,并重点介绍了如何使用时间戳进行增量数据同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.mongodb连接、字段等
查看上一篇
2.设置辅助增量要求如下
在这里插入图片描述
3.json格式如下

{
  "job": {
    "setting": {
      "speed": {
        "channel": 3,
        "byte": 1048576
      },
      "errorLimit": {
        "record": 0,
        "percentage": 0.02
      }
    },
    "content": [
      {
        "reader": {
          "name": "mongodbreader",
          "parameter": {
            "address": [
              "192.168.1.100:27017"
            ],
            "userName": "username",
            "userPassword": "password",
            "dbName": "DBname",
            "collectionName": "collectionname",
            "column": [
              {
                "name": "sourceSystem",
                "type": "String"
              },
              {
                "name": "configId",
                "type": "String"
              },
              {
                "name": "target",
                "type": "String"
              },
              {
                "name": "userName",
                "type": "String"
              },
              {
                "name": "paramJson",
                "type": "String"
              },
              {
                "name": "sendTime",
                "type": "Date"
              },
              {
                "name": "detailId",
                "type": "Long"
              }
            ],
            "query": "{\"sendTime\":{\"$gte\":ISODate(${lastTime}),\"$lt\":ISODate(${currentTime})},\"detailType\":\"app\",\"sourceSystem\":\"qfcoss\",\"success\": {\"$eq\": true}}"
          }
        },
        "writer": {
          "name": "mongodbwriter",
          "parameter": {
            "address": [
              "192.168.1.101:27017"
            ],
            "userName": "username",
            "userPassword": "password",
            "dbName": "DBname",
            "collectionName": "collectionname",
            "column": [
              {
                "name": "sourceSystem",
                "type": "String"
              },
              {
                "name": "app_code",
                "type": "String"
              },
              {
                "name": "target",
                "type": "String"
              },
              {
                "name": "user_name",
                "type": "String"
              },
              {
                "name": "content",
                "type": "String"
              },
              {
                "name": "send_time",
                "type": "Date"
              },
              {
                "name": "outer_id",
                "type": "Long"
              }
            ],
            "upsertInfo": {
              "isUpsert": false,
              "upsertKey": ""
            }
          }
        }
      }
    ]
  }
}

query筛选基本和mongodb的find方法类似 sendTime是时间格式筛选 , success是布尔值筛选 ,detailType是字符或者int筛选可以参考

如果有时间戳字段,增量时间段修改成 %s注意不要有单引号
在这里插入图片描述
query修改为

    "query": "{\"updateTimestamp\":{\"$gte\":${lastTime},\"$lt\":${currentTime}},\"detailType\":\"app\",\"sourceSystem\":\"qfcoss\",\"success\": {\"$eq\": true}}"
### DataX MySQL 到 MongoDB配置与实现 DataX 是阿里巴巴开源的一款离线数据同步工具,支持多种异构数据源之间的高效数据传输。以下是关于如何通过 DataX 将数据从 MySQL 迁移到 MongoDB 的具体配置和实现方法。 #### 配置说明 为了成功迁移数据,需要准备以下内容: 1. **MySQL 数据库连接信息** 确保已准备好 MySQL 数据库的相关信息,包括数据库地址、端口号、用户名、密码以及目标表名。通常可以通过编辑 `bootstrap.properties` 文件来设置这些参数[^1]。 2. **MongoDB 数据库连接信息** 同样需要提供 MongoDB连接信息,例如主机地址、端口、认证信息(如果启用了身份验证)、目标集合名称等。 3. **JSON 配置文件编写** DataX 使用 JSON 格式的配置文件定义任务的具体细节。下面是一个典型的从 MySQL 到 MongoDB配置模板。 #### 示例配置文件 ```json { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "your_mysql_username", // 替换为实际的 MySQL 用户名 "password": "your_mysql_password", // 替换为实际的 MySQL 密码 "column": ["*"], // 要读取的列,默认全量读取 "connection": [ { "jdbcUrl": ["jdbc:mysql://<db_host>:<port>/<database_name>"], // 替换为实际的 JDBC URL "table": ["source_table"] // 替换为目标表名 } ] } }, "writer": { "name": "mongodbwriter", "parameter": { "host": "<mongo_host>", // 替换为实际的 MongoDB 主机地址 "port": <mongo_port>, // 替换为实际的 MongoDB 端口号 "dbName": "target_database", // 替换为目标数据库名称 "collectionName": "target_collection",// 替换为目标集合名称 "writeMode": "insert", // 插入模式 (默认 insert) "batchSize": 100, // 批处理大小 "authSource": "", // 认证来源 DB 名称(如果有) "userName": "your_mongo_user", // 替换为实际的 MongoDB 用户名(可选) "password": "your_mongo_password" // 替换为实际的 MongoDB 密码(可选) } } } ], "setting": { "speed": { "channel": 3 // 并发通道数 } } } } ``` #### 实现步骤解析 - **Reader 部分**: 定义了从 MySQL 中读取数据的方式。需指定正确的 JDBC URL 和表结构。 - **Writer 部分**: 明确了写入到 MongoDB 的方式。注意调整 `writeMode` 参数以适应不同的业务场景。 - **Setting 部分**: 可调节并发性能,提升大规模数据迁移效率。 #### 注意事项 - 如果 MySQL 表中的字段类型无法直接映射至 MongoDB,请提前做好必要的转换逻辑。 - 对于超大数据集,建议分批次运行任务,避免内存溢出等问题。 - 若服务未安装 DataX 工具包,可通过手动下载并部署其二进制版本完成操作[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值