DataX分别使用Java代码实现和python代码实现

Jonderwu 2020-03-04

一、Java代码实现(代码出处:https://www.cnblogs.com/smartisn/p/12390964.html

package datax;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class CMD {
    Process process;
    public void CmdTest(String json) throws IOException {
        process=Runtime.getRuntime().exec("cmd");   //获取控制台
        new Thread(){
            public void run() {
                try {
                    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));  //获取输入流
                    bw.write("CHCP 65001");
                    bw.newLine();

                    bw.write("d:");
                    bw.newLine();

                    bw.write("cd D:\\BigDataTools\\datax\\datax\\bin");
                    bw.newLine();//因为读取时是一行行读取的,不加newline无法继续往下读

                    bw.write("python datax.py "+json);
                    bw.newLine();


                    bw.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();;
        new Thread() {
            public void run() {
                try {
                    BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String cmdout="";
                    while ((cmdout=br.readLine())!=null) {
                        System.out.println(cmdout);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();

    }

    public static void main(String[] args) throws IOException {
        CMD cmd=new CMD();
        String json="D:\\BigDataTools\\datax\\datax\\bin\\stream2stream.json";
        cmd.CmdTest(json);

    }
}

二、python代码实现(代码出处:https://www.cnblogs.com/smartisn/p/12391171.html

import os
if __name__==‘__main__‘:
    str="CHCP 65001"+"&&"
    str+="d:"+"&&"
    str += "cd D:\\BigDataTools\\datax\\datax\\bin" + "&&"
    str += "python datax.py " + "D:\\BigDataTools\\datax\\datax\\bin\\stream2stream.json"
    print(str)
    cmd=os.system(str)
    print(cmd)

三、总结

不管是Java代码实现DataX还是python代码实现DataX原理都是调用cmd界面调用DataX固定的接口实现数据导入。

相关推荐