按照视频课程,从Github上下载文件:https://github.com/alibaba/DataX
然后将下载的压缩包解压即可,不过需要的前提Python环境是要求python2,于是在python3的基础上,又安装了python2,配置了环境
打开cmd,进入到解压的目录bin目录下例如我的:
发现会有以下几个python文件
继续在命令行输入命令:
python2 datax.py ../job/csv_mysql.json
其中,用python2对datax.py进行编译,后面加上需要运行的json文件,代码如下:
{ "job": { "setting": { "speed": { "channel": 1 } }, "content": [ { "reader": { "name": "txtfilereader", "parameter": { "path": ["D:/yq.csv"], "encoding": "gbk", "column": [ { "index": 0, "type": "string" }, { "index": 1, "type": "string" }, { "index": 2, "type": "string" }, { "index": 3, "type": "string" } ], "fieldDelimiter": ",", "skipHeader": "true" } }, "writer": { "name": "mysqlwriter", "parameter": { "writeMode": "insert", "username": "root", "password": "root", "column": [ "pro", "yes", "dead", "ys" ], "session": [], "connection": [ { "jdbcUrl": "jdbc:mysql://localhost:3306/fy?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8", "table": ["yq2"] } ] } } } ] } }
在运行之前,需要在数据库中提前建好数据表,然后再执行。
需要注意的是,我在运行的时候出现了乱码现象,最终找到的解决方法是,在命令行(cmd)先输入CHCP 65001 然后就解决了乱码问题,运行结果如下: