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:如有不对的地方请大神及时指出。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,以下是使用DolphinScheduler自动生成工作流JSON的示例代码: ```java 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; public class WorkflowJsonGenerator { public static void main(String[] args) { // 读取工作流模板文件 File templateFile = new File("workflow_template.json"); StringBuilder templateContent = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(templateFile))) { String line; while ((line = reader.readLine()) != null) { templateContent.append(line); } } catch (Exception e) { e.printStackTrace(); } // 生成多个工作流JSON List<String> workflowJsonList = new ArrayList<>(); for (int i = 1; i <= 10; i++) { String workflowJson = templateContent.toString().replace("{{workflow_name}}", "Workflow " + i); workflowJsonList.add(workflowJson); } // 输出工作流JSON for (String workflowJson : workflowJsonList) { System.out.println(workflowJson); } } } ``` 这段代码做了以下几件事情: 1. 读取工作流模板文件(假设文件名为"workflow_template.json")的内容。 2. 根据模板内容生成多个工作流JSON,将模板中的"{{workflow_name}}"替换为具体的工作流名称。 3. 将生成工作流JSON输出到控制台。 请注意,这只是一个示例代码,你需要根据实际情况进行修改和适配。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值