GitHub
https://github.com/alibaba/DataX
https://github.com/alibaba/DataX/blob/master/introduction.md
https://github.com/alibaba/DataX/blob/master/userGuid.md
JDK安装
https://www.oracle.com/java/technologies/downloads/archive/
版本说明
Linux x86 RPM Package //适用于32bit的centos、rethat(linux)操作系统
Linux x64 RPM Package //适用于64bit的centos、rethat(linux)操作系统
Linux x86 Compressed Archive //适用于32bit的Linux操作系统
Linux x64 Compressed Archive //适用于64bit的Linux操作系统
简介
DataX 是阿里云 DataWorks数据集成 的开源版本,在阿里巴巴集团内被广泛使用的离线数据同步工具/平台。DataX 实现了包括 MySQL、Oracle、OceanBase、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、Hologres、DRDS 等各种异构数据源之间高效的数据同步功能。
本示例测试使用的sqlserver将一部分表全量同步到mysql。
背景需求
将某个sql server数据库的某个表的数据以离线作业方式同步到另外一个my sql数据库的表中.
添加mysql 驱动
在对mysql数据库进行读写的数据同步时,需要考虑mysql的数据库版本不同时,要自己手工添加上对应的mysql驱动版本.添加方法如下,先下载mysql-connector-java-8.0.27.jar,找到以下的目录(/usr/local是我自己定义的目录),
将mysql-connector-java-8.0.27.jar拷贝到以下目录下,这样就支持对mysql 8.0.27版本的写操作;
驱动版本下载: MySQL Community Downloads
/usr/local/datax/plugin/writer/mysqlwriter/libs/
再找到以下目录(/usr/local是我自己定义的目录),
/usr/local/datax/plugin/reader/mysqlreader/libs/
将mysql-connector-java-8.0.27.jar拷贝到以下目录下,这样就支持对mysql 8.0.27版本的读操作;
:::warning
注意:在win环境下,mysql-connector-java连接驱动,如果有多个版本同时存在,dataX会选择低版本的连接驱动使用,这时选择的的连接驱动不一定对应上你安装的MySql数据库版本,这时只能保留其中一个版本才能正常执行作业.但测试时,在Linux环境下时,如果存在多版本的连接驱动,程序又能正常执行,只要存在对应Mysql数据库版本的连接驱动就行.
:::
举例,我的mysql 是8.0.27版本,需要使用mysql-connector-java-8.0.27.jar,但如果同时存在着默认的mysql-connector-java-5.1.34.jar,执行作业就会报错
查看配置模板
python3 /usr/local/datax/bin/datax.py -r sqlserverreader -w mysqlwriter
{
"job": {
"content": [
{
"reader": {
"name": "sqlserverreader",
"parameter": {
"connection": [
{
"jdbcUrl": [],
"table": []
}
],
"password": "",
"username": ""
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [],
"connection": [
{
"jdbcUrl": "",
"table": []
}
],
"password": "",
"preSql": [],
"session": [],
"username": "",
"writeMode": ""
}
}
}
],
"setting": {
"speed": {
"channel": ""
}
}
}
}
配置文件模板
{
"job": {
"content": [
{
"reader": {
"name": "mysqlreader", # 读取端
"parameter": {
"column": [], # 需要同步的列 (* 表示所有的列)
"connection": [
{
"jdbcUrl": [], # 连接信息
"table": [] # 连接表
}
],
"password": "", # 连接用户
"username": "", # 连接密码
"where": "" # 描述筛选条件
}
},
"writer": {
"name": "mysqlwriter", # 写入端
"parameter": {
"column": [], # 需要同步的列
"connection": [
{
"jdbcUrl": "", # 连接信息
"table": [] # 连接表
}
],
"password": "", # 连接密码
"preSql": [], # 同步前. 要做的事
"session": [],
"username": "", # 连接用户
"writeMode": "" # 操作类型
}
}
}
],
"setting": {
"speed": {
"channel": "" # 指定并发数
}
}
}
}
配置文件1
cd /usr/local/datax/job/
vim mssqltomysql-published.json
{
"job": {
"content": [
{
"reader": {
"name": "sqlserverreader",
"parameter": {
"column": [
"Id",
"Version",
"Name",
"Content",
"Retries",
"Added",
"ExpiresAt",
"StatusName"
],
"connection": [
{
"jdbcUrl": [
"jdbc:sqlserver://192.168.3.40:1433;DatabaseName=es_test"
],
"table": [
"cap_published"
]
}
],
"password": "longfuchu",
"username": "sa"
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [
"Id",
"Version",
"Name",
"Content",
"Retries",
"Added",
"ExpiresAt",
"StatusName"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://192.168.3.40:3306/es_test?connectTimeout=10000&characterEncoding=utf-8&useSSL=false",
"table": [
"cap_published"
]
}
],
"password": "123456",
"preSql": [],
"session": [],
"username": "root",
"writeMode": "insert"
}
}
}
],
"setting": {
"speed": {
"channel": "5"
}
}
}
}
验证
python3 /usr/local/datax/bin/datax.py /usr/local/datax/job/mssqltomysql-published.json
如果出现以下错误提示
2022-10-10 12:56:04.693 [main] WARN Engine - prioriy set to 0, because NumberFormatException, the value is: null
2022-10-10 12:56:04.696 [main] INFO PerfTrace - PerfTrace traceId=job_-1, isEnable=false, priority=0
2022-10-10 12:56:04.697 [main] INFO JobContainer - DataX jobContainer starts job.
2022-10-10 12:56:04.699 [main] INFO JobContainer - Set jobId = 0
2022-10-10 12:56:05.311 [job-0] INFO OriginalConfPretreatmentUtil - Available jdbcUrl:jdbc:sqlserver://192.168.3.40:1433;DatabaseName=es_test.
2022-10-10 12:56:05.484 [job-0] INFO OriginalConfPretreatmentUtil - table:[cap_published] has columns:[Id,Version,Name,Content,Retries,Added,ExpiresAt,StatusName].
2022-10-10 12:56:05.724 [job-0] ERROR RetryUtil - Exception when calling callable, 异常Msg:Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
com.alibaba.datax.common.exception.DataXException: Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at com.alibaba.datax.common.exception.DataXException.asDataXException(DataXException.java:26) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.RdbmsException.asConnException(RdbmsException.java:23) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:394) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.connect(DBUtil.java:384) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.access$000(DBUtil.java:22) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:322) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil$3.call(DBUtil.java:319) ~[plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.call(RetryUtil.java:164) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil$Retry.doRetry(RetryUtil.java:111) ~[datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.common.util.RetryUtil.executeWithRetry(RetryUtil.java:30) [datax-common-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:319) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.DBUtil.getConnection(DBUtil.java:303) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.util.JdbcConnectionFactory.getConnecttion(JdbcConnectionFactory.java:27) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:105) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.dealColumnConf(OriginalConfPretreatmentUtil.java:140) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.util.OriginalConfPretreatmentUtil.doPretreatment(OriginalConfPretreatmentUtil.java:35) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.rdbms.writer.CommonRdbmsWriter$Job.init(CommonRdbmsWriter.java:41) [plugin-rdbms-util-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.plugin.writer.mysqlwriter.MysqlWriter$Job.init(MysqlWriter.java:31) [mysqlwriter-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.initJobWriter(JobContainer.java:704) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.init(JobContainer.java:304) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.job.JobContainer.start(JobContainer.java:113) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.start(Engine.java:92) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.entry(Engine.java:171) [datax-core-0.0.1-SNAPSHOT.jar:na]
at com.alibaba.datax.core.Engine.main(Engine.java:204) [datax-core-0.0.1-SNAPSHOT.jar:na]
2022-10-10 12:56:06.727 [job-0] ERROR RetryUtil - Exception when calling callable, 即将尝试执行第1次重试.本次重试计划等待[1000]ms,实际等待[1001]ms, 异常Msg:[Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.]
2022-10-10 12:56:08.733 [job-0] ERROR RetryUtil - Exception when calling callable, 即将尝试执行第2次重试.本次重试计划等待[2000]ms,实际等待[2000]ms, 异常Msg:[Code:[DBUtilErrorCode-10], Description:[连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境).]. - 具体错误信息为:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.]
^CTraceback (most recent call last):
File "/usr/local/datax/bin/datax.py", line 225, in <module>
(stdout, stderr) = child_process.communicate()
File "/usr/lib/python3.10/subprocess.py", line 1144, in communicate
self.wait()
File "/usr/lib/python3.10/subprocess.py", line 1207, in wait
return self._wait(timeout=timeout)
File "/usr/lib/python3.10/subprocess.py", line 1941, in _wait
(pid, sts) = self._try_wait(0)
File "/usr/lib/python3.10/subprocess.py", line 1899, in _try_wait
(pid, sts) = os.waitpid(self.pid, wait_flags)
File "/usr/local/datax/bin/datax.py", line 55, in suicide
print >> sys.stderr, "[Error] DataX receive unexpected signal %d, starts to suicide." % (signum)
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?
除了检查数据库的各项配置外,还需要注意是否因为是数据库的版本问题,如我的mysql数据库版本是8.0.27,但dataX默认的驱动版本是mysql-connector-java-5.1.34.jar,这里就需要把8.0.27版本的驱动程序也要添加到dataX里面
执行结果
配置文件2
{
"job": {
"content": [
{
"reader": {
"name": "sqlserverreader",
"parameter": {
"column": [
"id",
"deptno",
"deptname",
"bloodno",
"bloodname",
"boutcount",
"bloodunitname",
"bodate"
],
"connection": [
{
"jdbcUrl": [
"jdbc:sqlserver://192.168.3.40:1433;DatabaseName=es_test"
],
"table": [
"V_Blood_BOutItem"
]
}
],
"password": "longfuchu",
"username": "sa"
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": [
"id",
"deptno",
"deptname",
"bloodno",
"bloodname",
"boutcount",
"bloodunitname",
"bodate"
],
"connection": [
{
"jdbcUrl": "jdbc:mysql://192.168.3.40:3306/es_test?connectTimeout=10000&characterEncoding=utf-8&useSSL=false",
"table": [
"V_Blood_BOutItem"
]
}
],
"password": "123456",
"preSql": [],
"session": [],
"username": "root",
"writeMode": "insert"
}
}
}
],
"setting": {
"speed": {
"channel": "5"
}
}
}
}
验证
python3 /usr/local/datax/bin/datax.py /usr/local/datax/job/mssqltomysql-outitems.json