资源简介
java调用shell向DataX传递参数,where条件,包含特殊字符。java调用shell向DataX传递参数,where条件,包含特殊字符
代码片段和文件信息
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CommandExe {
public static void main(String[] args) {
try {
List command = new ArrayList<>();
//java调用shell命令并且传递带“的参数时,用sh -c 整个命令的方式
command.add(“sh“);
command.add(“-c“);
command.add(“python /opt/datax/bin/datax.py -p \“-DtableName=etl_job -DsqlWhere=LAST_ETL_DATE\\<\\=to_date\\(\\‘20170801\\‘\\‘yyyyMMdd\\‘\\)\“ /opt/datax/job/etl_job_oracle2postgresql_sqlwhere_full.json“);
//在etl_job_oracle2postgresql_sqlwhere_full.json中设置了tableName和sqlWhere 2个参数,用命令传参到json文件
//ps:where条件中不能有空格,并且shell中的特殊字符如 < ‘ ( 等要用\转义
StringBuffer sb=new StringBuffer();
for(String com:command){
sb.append(com+“ “);
}
System.out.println(sb.toString());
ProcessBuilder sqoopProcessBuilder = new ProcessBuilder(command);
Process process = sqoopProcessBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = ““;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
int exitValue = process.waitFor();
if (exitValue != 0) {
System.out.println(exitValue);
Exception e = new Exception();
throw e;
}else{
System.out.println(“==========successfully executed extract job==========“);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1730 2017-08-16 17:48 CommandExeHan.java
文件 1464 2017-08-16 11:02 etl_job_oracle2postgresql_sqlwhere_full_han.json
评论
共有 条评论