[大数据技术]datax的安装以及使用

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 }
View Code
  • "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 }
View Code

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 

posted @ 2020-02-27 20:10  雾霾王者  阅读(2129)  评论(0编辑  收藏  举报