一:介紹
DataX 是阿里巴巴集團內被廣泛使用的離線數據同步工具/平臺,實現包括 MySQL、Oracle、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、DRDS 等各種異構數據源之間高效的數據同步功能。
二:環境準備
1:Java 1.8.0_231
2.Python 3.6.5
3.MySQL:5.5.53
3.Datax:http://datax-opensource.oss-cn-hangzhou.aliyuncs.com/datax.tar.gz(約800M)
下載地址:https://github.com/alibaba/DataX頁面Download DataX下載地址
三:配置
1.準備數據:
MySQL
建庫:sourcedb
建表:t_user_info
CREATE TABLE `t_user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`content` text,
`createdate` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
導數:
INSERT INTO `sourcedb`.`t_user_info` (`id`, `name`, `content`, `createdate`) VALUES ('1', 'linux重啓mysql的命令', 'linux重啓mysql的命令', '2020-04-19 21:18:58');
INSERT INTO `sourcedb`.`t_user_info` (`id`, `name`, `content`, `createdate`) VALUES ('2', 'jQuery給input綁定回車事件', 'jQuery給input綁定回車事件', '2020-04-20 21:19:14');
INSERT INTO `sourcedb`.`t_user_info` (`id`, `name`, `content`, `createdate`) VALUES ('3', 'Web開發Session超時設置', 'Web開發Session超時設置', '2020-04-21 21:19:19');
建庫:targetdb
建表:t_user_info
CREATE TABLE `t_user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`content` text,
`createdate` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2.配置DataX
如要支持Python需要修改DataX默認py文件
下載:https://github.com/WeiYe-Jing/datax-web/tree/master/doc/datax-web/datax-python3
替換datax/bin/datax.py,datax/bindxprof.py,datax/binperftrace.py
3.準備執行文件:datax/task/mysql_3_mysql.json
{
"job": {
"setting": {
"speed": {
"channel": 1
}
},
"content": [
{
"reader": {
"name": "mysqlreader",
"parameter": {
"username": "root",
"password": "111111",
"column": [ "id", "name","content" ,"createdate"],
"splitPk": "id",
"connection": [
{
"table": [
"t_user_info"
],
"jdbcUrl": [
"jdbc:mysql://127.0.0.1:3306/sourcedb"
]
}
]
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"writeMode": "insert",
"username": "root",
"password": "111111",
"column": [ "id", "name","content","createdate"],
"session": [
"set session sql_mode='ANSI'"
],
"preSql": [
"delete from t_user_info"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://127.0.0.1:3306/targetdb",
"table": [
"config"
]
}
]
}
}
}
]
}
}
四:執行
cd datax
python .\bin\datax.py .\task\mysql-2-mysql.json
bytes | All Task WaitWriterTime 0.000s | All Task WaitReaderTime 0.000s | Percentage 100.00%
五:驗證
use targetdb;
select * from t_user_info;
sourcedb中t_user_info數據已經複製到targetdb的t_user_info表中