DataX | 使用脚本实现多表定时同步

1、创建syncData.sh脚本 

#!/bin/bash
. /etc/profile
# 读库的IP
r_ip="127.0.0.1"
# 读库的端口
r_port="3306"
# 读库的数据库名称
r_dbname="datax"
# 读库的账号
r_username="root"
# 读库的密码
r_password="123456"
# 写库的IP
w_ip="127.0.0.1"
# 写库的端口
w_port="3306"
# 写库的数据库名称
w_dbname="datax2"
# 写库的账号
w_username="root"
# 写库的密码
w_password="123456"
# DataX全量同步(多个文件直接写多个执行命令)
python /opt/datax/bin/datax.py /opt/datax/job/table1.json -p "-Dr_ip=$r_ip -Dr_port=$r_port -Dr_dbname=$r_dbname -Dr_username=$r_username -Dr_password=$r_password -Dw_ip=$w_ip -Dw_port=$w_port -Dw_dbname=$w_dbname -Dw_username=$w_username -Dw_password=$w_password"
python /opt/datax/bin/datax.py /opt/datax/job/table2.json -p "-Dr_ip=$r_ip -Dr_port=$r_port -Dr_dbname=$r_dbname -Dr_username=$r_username -Dr_password=$r_password -Dw_ip=$w_ip -Dw_port=$w_port -Dw_dbname=$w_dbname -Dw_username=$w_username -Dw_password=$w_password"

赋权:

chmod 777 syncData.sh

table1.json示例如下: 


{
    "job": {
        "content": [
            {
                "reader": {
                    "name": "oraclereader", 
                    "parameter": {
                        "column": ["ID","APP_NAME","DISPLAY_NAME"], 
                        "connection": [
                            {
                                "jdbcUrl": ["jdbc:oracle:thin:@${r_ip}:${r_port}:ORCL"], 
                                "table": ["T_APP"]
                            }
                        ], 
                        "password": "${r_password}", 
                        "username": "${r_username}"
                    }
                }, 
                "writer": {
                    "name": "mysqlwriter", 
                    "parameter": {
                        "column": ["id","app_name","display_name"], 
                        "connection": [
                            {
                                "jdbcUrl": "jdbc:mysql://${w_ip}:${w_port}/${w_dbname}", 
                                "table": ["t_app"]
                            }
                        ], 
                        "password": "${w_username}", 
                        "username": "${w_password}", 
                        "preSql":[],
                        "session":[],
                        "writeMode": "insert"
                    }
                }
            }
        ], 
        "setting": {
            "speed": {
                "channel": "6"
            }
        }
    }
}

然后执行sh文件即可

./syncData.sh

编写定时任务

crontab -e

 每天5点执行,并输出到日志文件中

0 5 * * * /opt/datax/bin/syncData.sh > /opt/datax/log/syncData_$(date +\%Y\%m\%d).log 2>&1

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,演示如何使用DataX实现多表数据同步到一个JSON文件中。 假设我们有两个表,一个是用户信息表(user),一个是订单信息表(order),它们的结构如下: user表: | id | name | age | | ---- | ---- | ---- | | 1 | Tom | 20 | | 2 | Jack | 22 | | 3 | Mary | 21 | order表: | id | user_id | product | amount | | ---- | ------- | ------- | ------ | | 1 | 1 | iPhone | 1 | | 2 | 1 | iPad | 2 | | 3 | 2 | MacBook | 1 | 我们需要将这两个表的数据同步到一个JSON文件中,格式如下: ``` { "users": [ { "id": 1, "name": "Tom", "age": 20, "orders": [ { "id": 1, "product": "iPhone", "amount": 1 }, { "id": 2, "product": "iPad", "amount": 2 } ] }, { "id": 2, "name": "Jack", "age": 22, "orders": [ { "id": 3, "product": "MacBook", "amount": 1 } ] }, { "id": 3, "name": "Mary", "age": 21, "orders": [] } ] } ``` 其中,每个用户包含其基本信息和订单信息,如果用户没有订单,则其订单列表为空。 为了实现这个需求,我们可以使用DataX中的两个插件:MySQL Reader插件和JSON Writer插件。 首先,我们需要在DataX的配置文件中配置MySQL Reader插件,使用SQL语句从数据库中读取数据。配置文件示例如下: ``` { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "connection": [ { "jdbcUrl": "jdbc:mysql://127.0.0.1:3306/test", "table": [ "user", "order" ], "username": "root", "password": "root" } ], "column": [ "user.id", "user.name", "user.age", "order.id", "order.product", "order.amount" ], "where": "", "splitPk": "", "encoding": "UTF-8" } }, "writer": null } ], "setting": { "speed": { "channel": 1 } } } } ``` 在该配置文件中,我们使用MySQL Reader插件,从MySQL数据库中读取了user和order表的数据。为了方便后续处理,我们将两个表的数据列合并到了一起。 接下来,我们需要在配置文件中配置JSON Writer插件,将数据写入到JSON文件中。配置文件示例如下: ``` { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "connection": [ { "jdbcUrl": "jdbc:mysql://127.0.0.1:3306/test", "table": [ "user", "order" ], "username": "root", "password": "root" } ], "column": [ "user.id", "user.name", "user.age", "order.id", "order.product", "order.amount" ], "where": "", "splitPk": "", "encoding": "UTF-8" } }, "writer": { "name": "jsonwriter", "parameter": { "path": "/data/users.json", "fileType": "NORMAL", "compress": "NONE", "encoding": "UTF-8", "dateFormat": "yyyy-MM-dd HH:mm:ss", "writeMode": "APPEND", "column": [ { "name": "id", "type": "LONG" }, { "name": "name", "type": "STRING" }, { "name": "age", "type": "LONG" }, { "name": "orders", "type": "ARRAY", "childColumn": [ { "name": "id", "type": "LONG" }, { "name": "product", "type": "STRING" }, { "name": "amount", "type": "LONG" } ] } ] } } } ], "setting": { "speed": { "channel": 1 } } } } ``` 在该配置文件中,我们使用了JSON Writer插件,将数据写入到了一个JSON文件中。我们将用户信息的id、name和age作为一级字段,将订单信息的id、product和amount作为二级字段,使用数组形式存储在orders字段中。 最后,我们使用DataX启动任务,即可将数据从MySQL数据库中读取,并写入到一个JSON文件中。 以上示例仅供参考,实际使用中需要根据具体需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值