一、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 }
二、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)
三、总结
不管是Java代码实现DataX还是python代码实现DataX原理都是调用cmd界面调用DataX固定的接口实现数据导入。
原文地址:https://www.cnblogs.com/goubb/p/12416625.html
时间: 2024-10-05 04:45:52