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

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

 1 package datax;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStreamWriter;
 8 
 9 public class CMD {
10     Process process;
11     public void CmdTest(String json) throws IOException {
12         process=Runtime.getRuntime().exec("cmd");   //获取控制台
13         new Thread(){
14             public void run() {
15                 try {
16                     BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));  //获取输入流
17                     bw.write("CHCP 65001");
18                     bw.newLine();
19 
20                     bw.write("d:");
21                     bw.newLine();
22 
23                     bw.write("cd D:\\BigDataTools\\datax\\datax\\bin");
24                     bw.newLine();//因为读取时是一行行读取的,不加newline无法继续往下读
25 
26                     bw.write("python datax.py "+json);
27                     bw.newLine();
28 
29 
30                     bw.flush();
31                 } catch (IOException e) {
32                     // TODO Auto-generated catch block
33                     e.printStackTrace();
34                 }
35             }
36         }.start();;
37         new Thread() {
38             public void run() {
39                 try {
40                     BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
41                     String cmdout="";
42                     while ((cmdout=br.readLine())!=null) {
43                         System.out.println(cmdout);
44                     }
45                 } catch (IOException e) {
46                     // TODO Auto-generated catch block
47                     e.printStackTrace();
48                 }
49             }
50         }.start();
51 
52     }
53 
54     public static void main(String[] args) throws IOException {
55         CMD cmd=new CMD();
56         String json="D:\\BigDataTools\\datax\\datax\\bin\\stream2stream.json";
57         cmd.CmdTest(json);
58 
59     }
60 }
View Code

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

1 import os
2 if __name__=='__main__':
3     str="CHCP 65001"+"&&"
4     str+="d:"+"&&"
5     str += "cd D:\\BigDataTools\\datax\\datax\\bin" + "&&"
6     str += "python datax.py " + "D:\\BigDataTools\\datax\\datax\\bin\\stream2stream.json"
7     print(str)
8     cmd=os.system(str)
9     print(cmd)
View Code

三、总结

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

posted @ 2020-03-04 22:19  余鱼瑜渔  阅读(2346)  评论(0编辑  收藏  举报