Java通过python命令执行DataX任务

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();
        }
    }

}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值