1、datax簡述
DataX 是阿里巴巴集團內被廣泛使用的離線數據同步工具/平台,實現包括 MySQL、Oracle、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、DRDS 等各種異構數據源之間高效的數據同步功能。
2、datax在windows下的簡單使用步驟
在命令提示符界面進入datax\bin目錄下,如CD E:\datax\bin
用Python2執行此處為json文件的路徑
注意:必須使用Python2!!!!!!
Python3會報以下錯誤:
File "datax.py", line 114
print readerRef
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(readerRef)?
原因是python2的print在python3中變為了print()函數。
因此可以安裝python2,並用 py -2 來執行python代碼,如:py -2 datax.py E:\New1.json 來執行python2的代碼
同理py -3執行python3的代碼。
3、datax簡單示例參考(json文件)
從CSV到MySQL數據庫

1 { 2 "job": { 3 "content": [ 4 { 5 "reader": { 6 "name": "txtfilereader", 7 "parameter": { 8 "path": ["E:/Yi211.csv"], 9 "encoding":"gbk", 10 "column": [ 11 { 12 "index": 0, 13 "type": "string" 14 }, 15 { 16 "index": 1, 17 "type": "string" 18 }, 19 { 20 "index": 2, 21 "type": "string" 22 }, 23 { 24 "index": 3, 25 "type": "string" 26 }, 27 { 28 "index": 4, 29 "type": "string" 30 }, 31 { 32 "index": 5, 33 "type": "string" 34 }, 35 ], 36 "fieldDelimiter":"," 37 } 38 }, 39 "writer": { 40 "name": "mysqlwriter", 41 "parameter": { 42 "column": [ 43 "shengfen", 44 "chengshi", 45 "quezhen", 46 "zhiyu", 47 "siwang", 48 "shijian" 49 ], 50 "connection": [ 51 { 52 "jdbcUrl": "jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf8", 53 "table": ["yi211"] 54 } 55 ], 56 "password": "364624", 57 "username": "root", 58 "preSql":[""], 59 "session":["set session sql_mode='ANSI'"], 60 "writeMode":"insert" 61 } 62 } 63 } 64 ], 65 "setting": { 66 "speed": { 67 "channel": "1" 68 } 69 } 70 } 71 }
- "path": ["E:/Yi211.csv"], 為要導入的csv文件路徑。
- "encoding":"gbk", 為讀取CSV文件的編碼格式,這里應注意可能導致中文亂碼!!!
- "column": 參考CSV文件列數進行(安裝示例格式)進行進行修改。
- "name": "mysqlwriter", 代表該部分代碼為寫入MySQL的部分,同理"name": "txtfilereader", 代表該部分代碼為讀文本的部分。
- "jdbcUrl": "jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf8", 參考自己連接數據庫的部分進行修改,localhost此處代表ip;3306代表端口;dbtest修改為數據庫名。characterEncoding代表寫入表的編碼格式
- "username": "root", 數據庫的用戶名。
- "password": "364624", 數據庫的密碼。
- "writer": 部分的 "parameter": 填寫MySQL數據表內的表頭。
這里給出MySQL到MySQL數據庫的部分json文件,與上部分同理。

1 { 2 "job": { 3 "content": [ 4 { 5 "reader": { 6 "name": "mysqlreader", 7 "parameter": { 8 "column": ["id","name","num"], 9 "connection": [ 10 { 11 "jdbcUrl": ["jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&rewriteBatchedStatements=true"], 12 "table": ["test1"] 13 } 14 ], 15 "password": "364624", 16 "username": "root", 17 18 } 19 }, 20 "writer": { 21 "name": "mysqlwriter", 22 "parameter": { 23 "column": ["id","name","num"], 24 "connection": [ 25 { 26 27 "jdbcUrl": "jdbc:mysql://localhost:3306/dbtest", 28 "table": ["test2"] 29 } 30 ], 31 "password": "364624", 32 "username": "root", 33 } 34 } 35 } 36 ], 37 "setting": { 38 "speed": { 39 "channel": 1 40 } 41 } 42 } 43 }
4、可能出現的錯誤
json文件格式錯誤:
經DataX智能分析,該任務最可能的錯誤原因是:
com.alibaba.datax.common.exception.DataXException: Code:[Common-00], Describe:[您提供的配置文件存在錯誤信息,請檢查您的作業配置 .] - 配置信息錯誤. 您提供的配置信息不是合法的JSON格式: unclosed string : * . 請按照標准json格式提供配置信息.
命令提示符界面出現出現亂碼:
可以在cmd中輸入: CHCP 65001 ,然后進行后續操作。
py -2 datax.py E:\New1.json