如何上手DataX

介绍

DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL、Oracle、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、DRDS 等各种异构数据源之间高效的数据同步功能。

最好的学习和使用的文档地址 https://github.com/alibaba/DataX

已支持功能

类型 数据源 Reader(读) Writer(写) 文档
RDBMS 关系型数据库 MySQL
Oracle
SQLServer
PostgreSQL
DRDS
通用RDBMS(支持所有关系型数据库)
阿里云数仓数据存储 ODPS
ADS
OSS
OCS
NoSQL数据存储 OTS
Hbase0.94
Hbase1.1
Phoenix4.x
Phoenix5.x
MongoDB
Hive
Cassandra
无结构化数据存储 TxtFile
FTP
HDFS
Elasticsearch
时间序列数据库 OpenTSDB
TSDB

环境准备

  • 熟悉常见的maven命令和具备maven环境(此步骤是在官网安装包没有我们所需插件的前提下)
  • 具备python2环境(如果你是python3,将bin目录下的这三个脚本替换掉)

具体操作

  • 首先到官网进行下载安装包

  • 检测安装包中的plugin中的writer和reader是否有我们需要的

    • 如果有,那么可以进入到job目录直接修改配置文件了
    • 如果没有,那么需要到官网将源码下载下来,然后找到pom.xml中的writerreader注释位置,保留你需要的writerreader的module,讲其他的writerreader中对应的module注释掉即可
      • 注释完成后,在pom.xml的同级目录下使用mvn package命令进行编译打包
      • 编译打包完成后,进入到target目录下的datax目录下,在看plugin中的writer和reader已经出现了我们需要的writer和reader

当然你也可以不修改pom.xml中的module,将下载下来的源码直接进行编译打包,那么最终会包含datax所有的writer和reader(此包比较大)

如何配置

  • 修改datax解压目录下的job中的配置文件

  • 至于配置这里不再详述,上述表格中的 对应的链接中的demo配置选项已经写得非常详细了

如何运行

  • 进入到datax的bin目录下执行命令python datax.py {YOUR_JOB.json}

例如:csv导入到MySQL配置文件

{
    "setting": {},
    "job": {
        "setting": {
            "speed": {
                "channel": 5
            },
            "errorLimit": {
                "record": 0,
                "percentage": 0.02
            }
        },
        "content": [
            {
                "reader": {
                    "name": "txtfilereader",
                    "parameter": {
                        "path": ["E:\test.csv"],
                        "encoding": "UTF-8",
                        "fieldDelimiter": ",",
						            "skipHeader": "true",
                        "nullFormat": "",
                        "column": [
                            {
                                "index": 0,
                                "type": "string"
                            },
                            {
                                "index": 1,
                                "type": "string"
                            },
                            {
                                "index": 2,
                                "type": "string"
                            },
                            {
                                "index": 3,
                                "type": "long"
                            },
                            {
                                "index": 4,
                                "type": "string"
                            },
                            {
                                "index": 5,
                                "type": "date"
                            },
                            {
                                "index": 6,
                                "type": "date"
                            },
                            {
                                "index": 7,
                                "type": "string"
                            },
                            {
                                "index": 8,
                                "type": "string"
                            },
                            {
                                "index": 9,
                                "type": "string"
                            },
                            {
                                "index": 10,
                                "type": "date"
                            },
                            {
                                "index": 11,
                                "type": "string"
                            },
                            {
                                "index": 12,
                                "type": "date"
                            },
                            {
                                "index": 13,
                                "type": "string"
                            },
                            {
                                "index": 14,
                                "type": "date"
                            }
                        ]
                    }
                },
                "writer": {
                    "name": "mysqlwriter",
                    "parameter": {
                        "writeMode": "insert",
                        "username": "root",
                        "password": "123456",
                        "print": "False",
                        "column": [
                            "XH",
                            "YF",
                            "JYJLS",
                            "DATA_UNID",
                            "DATA_UUID",
                            "DATA_CDATE",
                            "DATA_UDATE",
                            "BASEID",
                            "DATA_BAT",
                            "DATA_STATE",
                            "IMPTIME",
                            "COMRESULT",
                            "COMTIME",
                            "CLERESULT",
                            "CLETIME"
                        ],
                        "connection": [
                            {
                                "jdbcUrl": "jdbc:mysql://localhost:3306/datax?useUnicode=true&characterEncoding=utf-8",
                                "table": [
                                    "JTJ_WYCYDDQK"
                                ]
                            }
                        ],
                    }
                }
            }
        ]
    }
}

狭路相逢勇者胜!
原文地址:https://www.cnblogs.com/amcoder/p/13919428.html