Java通过python命令执行DataX任务的实例

时间:2021-05-20

1.安装datax

2.安装python并配置环境变量

3.把mysql2odps.json文件放在datax安装目录的job文件夹下

4.运行Test.java测试

mysql2odps.json文件:

{ "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "$username", "password": "$password", "where": "optime>='$startDate $startTime' and optime<'$endDate $endTime'", "column": ["id","xm"], "connection": [ { "table": [ "test" ], "jdbcUrl": [ "$jdbcUrl" ] } ] } }, "writer": { "name": "odpswriter", "parameter": { "accessId": "****************", "accessKey": "******************************", "column": ["id","xm"], "odpsServer": "http://service.odps.xxx.com/api", "partition": "bt=$bt,region=$region", "project": "dst_project_name", "table": "test" } } } ], "setting": { "speed": { "channel": 10 } } }}

测试程序(我的datax安装路径为F盘根目录):

import java.io.BufferedReader;import java.io.InputStreamReader;public class Test { public static void main(String[] args) { try { System.out.println("start"); String windowcmd = "cmd /c python F:/datax/bin/datax.py -p \" -Dusername=root -Dpassword=1234 -DjdbcUrl=jdbc:mysql://192.168.1.122:3306/center -Dwhere=left(optime,10)=CURDATE() -Dbt=20171214 -Dregion=beijing \" F:/datax/job/mysql2odps.json"; System.out.println(windowcmd); Process pr = Runtime.getRuntime().exec(windowcmd); BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); pr.waitFor(); System.out.println("end"); } catch (Exception e) { e.printStackTrace(); } }}

以上这篇Java通过python命令执行DataX任务的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章