1、生成DataX json模板
public static void josnTemplate(String source,String target,String datasetId) {
String classPath = "D:/software/datax";
String jobFilePath = "D:/software/datax/job/";
System.setProperty("datax.home", classPath);
String[] arguments = new String[]{"python", classPath+"/bin/datax.py", "-r", source.toLowerCase()+"reader", "-w", target.toLowerCase()+"writer"};
try {
Process process = Runtime.getRuntime().exec(arguments);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(jobFilePath + "dataset_"+datasetId), "UTF-8"));
String line = null;
int count = 0;
while ((line = br.readLine()) != null) {
count++;
if (count > 15) {
bw.write(line);
bw.newLine();
bw.flush();
}
}
bw.close();
br.close();
int re = process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
2、组装DataX json模板
public static String getJson(String filePath) {
String json = "";
try {
File file = new File(filePath);
FileReader reader = new FileReader(file);
FileInputStream inputStream = new FileInputStream(file);
Reader stream = new InputStreamReader(inputStream, Charset.defaultCharset());
int ch = 0;
StringBuilder sb = new StringBuilder();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close();
stream.close();
JSONObject jobj = JSONObject.parseObject(sb.toString());
json = jobj.toJSONString();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
public static String getDataXJson(String filePath) throws FileNotFoundException {
String json = getJson(filePath);
JSONObject jobj = JSONObject.parseObject(json);
JSONObject reader = jobj.getJSONObject("job").getJSONArray("content").getJSONObject(0).getJSONObject("reader");
JSONObject rp = reader.getJSONObject("parameter");
rp.put("username", "root");
rp.put("password", "123456");
rp.remove("column");
JSONObject rc = rp.getJSONArray("connection").getJSONObject(0);
JSONArray jdbcUrl = new JSONArray();
jdbcUrl.add("jdbc:mysql://IP:3306/dataset?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai");
rc.put("jdbcUrl", jdbcUrl);
rc.remove("table");
JSONArray querySql = new JSONArray();
querySql.add("select uuid() as fusion_uuid,a,b,c,d,e,f,h from t_number");
rc.put("querySql", querySql);
JSONObject writer = jobj.getJSONObject("job").getJSONArray("content").getJSONObject(0).getJSONObject("writer");
JSONObject wp = writer.getJSONObject("parameter");
wp.put("username", "root");
wp.put("password", "123456");
JSONArray column = new JSONArray();
column.add("*");
wp.put("column", column);
JSONArray preSql = new JSONArray();
preSql.add("truncate table dataset_1619888763184439297");
wp.put("preSql", preSql);
JSONArray loadUrl = new JSONArray();
loadUrl.add("IP:8030");
wp.put("loadUrl", loadUrl);
JSONObject wc = wp.getJSONArray("connection").getJSONObject(0);
wc.put("jdbcUrl", "jdbc:mysql://IP:9030/fusion");
JSONArray table = new JSONArray();
table.add("dataset_1619888763184439297");
wc.put("table", table);
wc.put("selectedDatabase", "fusion");
JSONObject setting = jobj.getJSONObject("job").getJSONObject("setting");
JSONObject speed = setting.getJSONObject("speed");
speed.put("channel", "1");
JSONObject errorlimit = new JSONObject();
errorlimit.put("percentage",0.02);
errorlimit.put("record",0);
setting.put("errorLimit",errorlimit);
PrintStream stream= new PrintStream(filePath,"UTF-8");
return jobj.toString();
}
3、执行DataX job
public static void execute(String filePath) throws Exception {
String dataxHome = "D:/software/datax";
System.setProperty("datax.home", dataxHome);
String dataXJson = getDataXJson(filePath);
String[] dataXArgs = {"-job", filePath, "-mode", "standalone", "-jobid", "-1"};
try {
Engine.entry(dataXArgs);
}catch (DataXException e){
e.printStackTrace();
}
catch (Throwable throwable) {
log.error("\n\n经DataX智能分析,该任务最可能的错误原因::\n" + ExceptionTracker.trace(throwable));
}
}
public static void main(String[] args) throws Exception {
josnTemplate("mysql","doris","1619879625037537281");
getDataXJson("D:/software/datax/job/dataset_1619879625037537281");
execute("D:/software/datax/job/dataset_1619879625037537281");
}