关于云计算:DolphinScheduler-调度-DataX-实现-MySQL-To-MySQL-增量数据同步实战

8次阅读

共计 2811 个字符,预计需要花费 8 分钟才能阅读完成。

背景

MySQL 库 A 到 MySQL 库 B 的增量数据同步需要

DolphinScheduler 中配置 DataX MySQL To MySQL 工作流

工作流定义

工作流定义 > 创立工作流 > 拖入 1 个 SHELL 组件 > 拖入 1 个 DATAX 组件
SHELL 组件(文章)
脚本

echo '文章同步 MySQL To MySQL'

DATAX 组件 (t_article)
用到 2 个插件 mysqlreader^[1]、mysqlwriter^[2]
选 自定义模板:

{
    "job": {
        "content": [
            {
                "reader": {
                    "name": "mysqlreader",
                    "parameter": {
                        "connection": [
                            {
                                "jdbcUrl": ["jdbc:mysql://${biz_mysql_host}:${biz_mysql_port}/ 你的数据库 A?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8&autoReconnect=true&useSSL=false&&allowLoadLocalInfile=false&autoDeserialize=false&allowLocalInfile=false&allowUrlInLocalInfile=false"
                                ],
                                "querySql": ["select a.id,a.title,a.content,a.is_delete,a.delete_date,a.create_date,a.update_date from t_article a.update_date >='${biz_update_dt}';"
                                ]
                            }
                        ],
                        "password": "${biz_mysql_password}",
                        "username": "${biz_mysql_username}"
                    }
                },
                "writer": {
                    "name": "mysqlwriter",
                    "parameter": {
                        "column": [
                            "`id`",
                            "`title`",
                            "`content`",
                            "`is_delete`",
                            "`delete_date`",
                            "`create_date`",
                            "`update_date`"
                        ],
                        "connection": [
                            {"jdbcUrl": "jdbc:mysql://${biz_mysql_host}:${biz_mysql_port}/ 你的数据库 B?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8&autoReconnect=true&useSSL=false&&allowLoadLocalInfile=false&autoDeserialize=false&allowLocalInfile=false&allowUrlInLocalInfile=false",
                                "table": ["t_article"]
                            }
                        ],
                        "writeMode": "replace",
                        "password": "${biz_mysql_password}",
                        "username": "${biz_mysql_username}"
                    }
                }
            }
        ],
        "setting": {
            "errorLimit": {
                "percentage": 0,
                "record": 0
            },
            "speed": {
                "channel": 1,
                "record": 1000
            }
        }
    }
}

reader 和 writer 的字段配置需保持一致

自定义参数:

biz_update_dt: ${global_bizdate}
biz_mysql_host: 你的 mysql ip
biz_mysql_port: 3306
biz_mysql_username: 你的 mysql 账号
biz_mysql_password: 你的 mysql 明码

# 本文试验环境 A 库和 B 库用的同一个实例,如果 MySQL 是多个实例,能够再新减少参数定义例如 biz_mysql_host_b, 在模板中对应援用即可

配置的自定义参数将会主动替换 json 模板中的同名变量

reader mysqlreader 插件中要害配置:a.update_date >= '${biz_update_dt}' 就是实现增量同步的要害配置
writer mysqlwriter 插件中要害配置:“

"parameter": {
    "writeMode": "replace",
    ......
}

writeMode 为 replace,雷同主键 id 反复写入数据,就会更新数据。sql 实质上执行的是 replace into

保留工作流

全局变量设置
global_bizdate:$[yyyy-MM-dd 00:00:00-1]

global_bizdate 援用的变量为 DolphinScheduler 内置变量,具体参考官网文档 ^[3]
联合调度工夫设计好工夫滚动的窗口时长,比方按 1 天增量,那么这里工夫就是减 1 天

最终的工作流 DAG 图为:

爬坑记录

  • 官网下载的 DataX 不蕴含 ElasticSearchWriter 写插件
    默认不带该插件,须要本人编译 ElasticSearchWriter 插件。

    git clone https://github.com/alibaba/DataX.git

    为了放慢编译速度,能够只编译 <module>elasticsearchwriter</module>
    我的项目根目录的 pom.xml
    <!-- reader --> 全正文掉,<!-- writer -->下只保留 <module>elasticsearchwriter</module> 其余正文掉,另外 <!-- common support module --> 也须要保留

如果本人不想编译或者编译失败请搜寻🔍 “ 流水理鱼 ” 微信公众号,或者加我私人微信我给你曾经编译好的插件包

by 流水理鱼 |wwek

参考

1. DataX MysqlReader 插件文档 https://github.com/alibaba/DataX/blob/master/mysqlreader/doc/mysqlreader.md
2. DataX MysqlWriter 插件文档 https://github.com/alibaba/DataX/blob/master/mysqlwriter/doc/mysqlwriter.md
3. Apache DolphinScheduler 内置参数 https://dolphinscheduler.apache.org/zh-cn/docs/latest/user_doc/guide/parameter/built-in.html
本文首发于流水理鱼博客,如要转载请注明出处。
欢送关注我的公众号:流水理鱼(liushuiliyu),全栈、云原生、Homelab 交换。
如果您对相干文章感兴趣,也能够关注我的博客:www.iamle.com 下面有更多内容

正文完
 0