2020年8月-DolphinScheduler(原EasyScheduler)-自动生成项目下导入工作流json
可以直接生成json 导入到工作流。
创建的txt文件按行罗列,不要有末尾空格
代码如下
createWorkflow类
import java.io.File;
import org.hamcrest.core.SubstringMatcher;
public class createWorkflow {
//存放表列表的txt文件名
private static String filename = "\\work.txt";
//项目名称
private static String projectName = "项目测试";
//工作流名称
private static String workflow = "test1";
static File file=new File("");
static String path=file.getAbsolutePath()+filename;
static getTxtValue a = new getTxtValue();
public static void main(String[] args) {
System.out.println(path);//绝对路径
//获取文件列表内容
String value = a.readTxt(path);
//去掉末尾逗号
value = value.substring(0, value.length()-1);
System.out.println(value);//打印内容
String[] joblist = value.split(",");
StringBuffer threeValue = new StringBuffer();
StringBuffer threeValueEnd = new StringBuffer();
StringBuffer fiveValue = new StringBuffer();
StringBuffer sixValue = new StringBuffer();
int x = 100;
int endx = (joblist.length)*200/2;
String y = "100";
for (int i = 0; i < joblist.length; i++) {
System.out.println(joblist[i]);
threeValue.append("{\\\"workerGroupId\\\":-1,\\\"description\\\":\\\"\\\",\\\"runFlag\\\":\\\"NORMAL\\\",\\\"type\\\":\\\"SHELL\\\","
+ "\\\"params\\\":{\\\"rawScript\\\":\\\"echo \\\\\\\""+joblist[i]+"\\\\\\\"\\\",\\\"localParams\\\":[],\\\"resourceList\\\":[]},\\\"timeout\\\":{\\\"enable\\\":false,\\\"strategy\\\":\\\"\\\"},\\\"maxRetryTimes\\\":\\\"0\\\",\\\"taskInstancePriority\\\":\\\"MEDIUM\\\",\\\"name\\\":\\\""+joblist[i]+"\\\",\\\"dependence\\\":{},\\\"retryInterval\\\":\\\"1\\\",\\\"preTasks\\\":[],\\\"id\\\":\\\""+joblist[i]+"\\\"}");
threeValue.append(",");
threeValueEnd.append("\\\""+joblist[i]+"\\\",");
fiveValue.append("\\\""+joblist[i]+"\\\":{\\\"name\\\":\\\""+joblist[i]+"\\\",\\\"targetarr\\\":\\\"\\\",\\\"x\\\":"+x+",\\\"y\\\":"+y+"}");
fiveValue.append(",");
sixValue.append("{\\\"endPointSourceId\\\":\\\""+joblist[i]+"\\\",\\\"endPointTargetId\\\":\\\"task_end\\\"}");
if(i!=joblist.length-1){
threeValueEnd.append(",");
sixValue.append(",");
}
x=x+200;
}
String thee = threeValue.toString();
String theeEnd = threeValueEnd.toString();
String five = fiveValue.toString();
String six = sixValue.toString();
String json =
//1 项目名称
"{\"projectName\":\""+projectName+"\","
//2 工作流名称
+ "\"processDefinitionName\":\""+workflow+"\","
//3
+ "\"processDefinitionJson\":\"{\\\"tenantId\\\":-1,\\\"globalParams\\\":[],\\\"tasks\\\":["
+ ""+thee+""
+ "{\\\"workerGroupId\\\":-1,\\\"description\\\":\\\"\\\",\\\"runFlag\\\":\\\"NORMAL\\\",\\\"type\\\":\\\"SHELL\\\","
+ "\\\"params\\\":{\\\"rawScript\\\":\\\"echo \\\\\\\"task_end\\\\\\\"\\\",\\\"localParams\\\":[],\\\"resourceList\\\":[]},\\\"timeout\\\":{\\\"enable\\\":false,\\\"strategy\\\":\\\"\\\"},\\\"maxRetryTimes\\\":\\\"0\\\",\\\"taskInstancePriority\\\":\\\"MEDIUM\\\",\\\"name\\\":\\\"task_end\\\",\\\"dependence\\\":{},\\\"retryInterval\\\":\\\"1\\\",\\\"preTasks\\\":["+theeEnd+"],\\\"id\\\":\\\"task_end\\\"}"
+ "],\\\"timeout\\\":0}\","
//4
+ "\"processDefinitionDescription\":\"\","
//5
+ "\"processDefinitionLocations\":\"{"+five+""
+ "\\\"task_end\\\":{\\\"name\\\":\\\"task_end\\\",\\\"targetarr\\\":\\\""+value+"\\\",\\\"x\\\":"+endx+",\\\"y\\\":350}}\","
//6
+ "\"processDefinitionConnects\":\"["+six+"]\"}";
System.out.println(json);
// String json =
// //1 项目名称
// "{\"projectName\":\""+projectName+"\","
// //2 工作流名称
// + "\"processDefinitionName\":\""+workflow+"\","
// //3
// + "\"processDefinitionJson\":\"{\\\"tenantId\\\":-1,\\\"globalParams\\\":[],\\\"tasks\\\":["
// + "{\\\"workerGroupId\\\":-1,\\\"description\\\":\\\"\\\",\\\"runFlag\\\":\\\"NORMAL\\\",\\\"type\\\":\\\"SHELL\\\","
// + "\\\"params\\\":{\\\"rawScript\\\":\\\"echo \\\\\\\"test1\\\\\\\"\\\",\\\"localParams\\\":[],\\\"resourceList\\\":[]},\\\"timeout\\\":{\\\"enable\\\":false,\\\"strategy\\\":\\\"\\\"},\\\"maxRetryTimes\\\":\\\"0\\\",\\\"taskInstancePriority\\\":\\\"MEDIUM\\\",\\\"name\\\":\\\"job1\\\",\\\"dependence\\\":{},\\\"retryInterval\\\":\\\"1\\\",\\\"preTasks\\\":[],\\\"id\\\":\\\"tasks-7190\\\"},"
// + "{\\\"workerGroupId\\\":-1,\\\"description\\\":\\\"\\\",\\\"runFlag\\\":\\\"NORMAL\\\",\\\"type\\\":\\\"SHELL\\\","
// + "\\\"params\\\":{\\\"rawScript\\\":\\\"echo \\\\\\\"test2\\\\\\\"\\\",\\\"localParams\\\":[],\\\"resourceList\\\":[]},\\\"timeout\\\":{\\\"enable\\\":false,\\\"strategy\\\":\\\"\\\"},\\\"maxRetryTimes\\\":\\\"0\\\",\\\"taskInstancePriority\\\":\\\"MEDIUM\\\",\\\"name\\\":\\\"job2\\\",\\\"dependence\\\":{},\\\"retryInterval\\\":\\\"1\\\",\\\"preTasks\\\":[],\\\"id\\\":\\\"tasks-85994\\\"},"
// + "{\\\"workerGroupId\\\":-1,\\\"description\\\":\\\"\\\",\\\"runFlag\\\":\\\"NORMAL\\\",\\\"type\\\":\\\"SHELL\\\","
// + "\\\"params\\\":{\\\"rawScript\\\":\\\"echo \\\\\\\"task_end\\\\\\\"\\\",\\\"localParams\\\":[],\\\"resourceList\\\":[]},\\\"timeout\\\":{\\\"enable\\\":false,\\\"strategy\\\":\\\"\\\"},\\\"maxRetryTimes\\\":\\\"0\\\",\\\"taskInstancePriority\\\":\\\"MEDIUM\\\",\\\"name\\\":\\\"task_end\\\",\\\"dependence\\\":{},\\\"retryInterval\\\":\\\"1\\\",\\\"preTasks\\\":[\\\"job2\\\",\\\"job1\\\"],\\\"id\\\":\\\"task_end\\\"}"
// + "],\\\"timeout\\\":0}\","
// //4
// + "\"processDefinitionDescription\":\"\","
// //5
// + "\"processDefinitionLocations\":\"{\\\"tasks-7190\\\":{\\\"name\\\":\\\"job1\\\",\\\"targetarr\\\":\\\"\\\",\\\"x\\\":284,\\\"y\\\":145},"
// + "\\\"tasks-85994\\\":{\\\"name\\\":\\\"job2\\\",\\\"targetarr\\\":\\\"\\\",\\\"x\\\":288,\\\"y\\\":277},"
// + "\\\"task_end\\\":{\\\"name\\\":\\\"task_end\\\",\\\"targetarr\\\":\\\"tasks-85994,tasks-7190\\\",\\\"x\\\":467,\\\"y\\\":217}}\","
// //6
// + "\"processDefinitionConnects\":\"[{\\\"endPointSourceId\\\":\\\"tasks-85994\\\",\\\"endPointTargetId\\\":\\\"task_end\\\"},"
// + "{\\\"endPointSourceId\\\":\\\"tasks-7190\\\",\\\"endPointTargetId\\\":\\\"task_end\\\"}]\"}";
}
}
----------------------------------------------------------------------------
getTxtValue 类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.springframework.ui.ModelMap;
public class getTxtValue {
/**
* @Title: readTxt
* @Description: 读取txt文件内容
* @param filePath
* @return
*/
public static String readTxt(String filePath) {
StringBuilder result = new StringBuilder();
try {
// BufferedReader bfr = new BufferedReader(new FileReader(new File(filePath)));
BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));
String lineTxt = null;
while ((lineTxt = bfr.readLine()) != null) {
result.append(lineTxt).append(",");
}
bfr.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
public static void main(String[] args) {
int x = 100;
System.out.println("asdfhksajdfhkj"+x+"sahfk");
}
}
-------------------------------------
然后项目根目录创建一个TXT文件
PS:如有不对的地方请大神及时指出。