• 通过javabean生成springboot可用的yml



    使用场景

    项目中遇到了一个问题,做nebula图数据库的导入,由于导入工具需要标准的yml文件,作为导入的配置,而这个yml文件是根据导入的具体数据(csv的列) 生成的,也就是动态的,所以并不是一个死的文件,每次导入都需要生成这个文件,因此这个需求应运而生;


    一、构造要生成yml的bean?

    虽然每次都要生成,有些字段有所不同,但是整体的yml必要元素是确定,只不过有些为空,有些为集合而已,下面是我构造的bean;

    @Data
    public class TemplateBean {
    
        private String version = "v3";
        private String description = "web console import";
        private String removeTempFiles;
        private ClientSettings clientSettings;
        private String logPath = "/data1/opt/import.log";
        private List files;
    
    
        @Data
        public static class File {
            private String path = "/data1/opt/item.csv";
            private String failDataPath = "/data1/opt/itemFail.csv";
            private Integer batchSize = 60;
            private String limit;
            private String inOrder;
            private String type = "csv";
            private CSV csv;
            private Schema schema;
        }
    
        @Data
        public static class CSV {
            private Boolean withHeader = false;
            private Boolean withLabel = false;
            private String delimiter;
        }
    
        @Data
        public static class Schema {
            private String type = "vertex";
            private Vertex vertex;
            private Edge edge;
        }
    
        @Data
        public static class Edge {
            private String name = "order";
            private Boolean withRanking = false;
            private List props;
            private SrcDst srcVID;
            private SrcDst dstVID;
            private Integer rank;
        }
    
    
        @Data
        public static class SrcDst {
            private Integer index = 1;
            private String function = "ceshi";
            private String type = "string";
            private String prefix = "ceshi";
        }
    
    
        @Data
        public static class Vertex {
            private Vid vid;
            private List tags;
        }
    
    
        @Data
        public static class Vid {
            private Integer index = 0;
            private String function;
            private String type = "string";
            private String prefix;
        }
    
    
        @Data
        public static class Tag {
            private String name = "item";
            private List props;
        }
    
        @Data
        public static class Prop {
            private String name = "id_single_item";
            private String type = "string";
            private Integer index = 0;
        }
    
    
        @Data
        public static class ClientSettings {
            private Integer retry = 3;
            private Integer concurrency = 10;
            private Integer channelBufferSize = 128;
            private String space = "dataImport";
            private String postStart;
            private String preStop;
            private Connection connection;
        }
    
        @Data
        public static class Connection {
            private String user = "root";
            private String password = "nebula";
            private String address = "127.0.0.1:9669";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    可以看到,我这里面设置了一些默认值;

    二、根据已有bean,设置值,然后生成yml文件

    1.引入pom

            
                org.yaml
                snakeyaml
                1.27
            
    
            
                com.fasterxml.jackson.dataformat
                jackson-dataformat-yaml
                2.12.0
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    代码如下(示例):

    public class TemplateBeanMain {
        public static void main(String[] args) throws IOException {
    
            TemplateBean templateBean = new TemplateBean();
            TemplateBean.ClientSettings clientSettings = new TemplateBean.ClientSettings();
            clientSettings.setConnection(new TemplateBean.Connection());
            templateBean.setClientSettings(clientSettings);
    
            TemplateBean.File file = new TemplateBean.File();
            file.setCsv(new TemplateBean.CSV());
            TemplateBean.Schema schema = new TemplateBean.Schema();
            // 点
            TemplateBean.Vertex vertex = new TemplateBean.Vertex();
            vertex.setVid(new TemplateBean.Vid());
            TemplateBean.Tag tag = new TemplateBean.Tag();
            tag.setProps(CollectionUtil.newArrayList(new TemplateBean.Prop(),new TemplateBean.Prop(),new TemplateBean.Prop()));
            vertex.setTags(CollectionUtil.newArrayList(tag));
            schema.setVertex(vertex);
            // 边
            //TemplateBean.Edge edge = new TemplateBean.Edge();
            //edge.setDstVID(new TemplateBean.SrcDst());
            //edge.setSrcVID(new TemplateBean.SrcDst());
            //edge.setProps(CollectionUtil.newArrayList(new TemplateBean.Prop(),new TemplateBean.Prop(),new TemplateBean.Prop()));
            //schema.setEdge(edge);
            file.setSchema(schema);
            templateBean.setFiles(CollectionUtil.newArrayList(file));
    
    
            DumperOptions dumperOptions = new DumperOptions();
            dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            Yaml yaml = new Yaml(dumperOptions);
            yaml.dump(templateBean, new FileWriter("templateBean.yaml"));
            TemplateBean bean = yaml.loadAs(new FileReader("templateBean.yaml"), TemplateBean.class);
            System.out.println(JSONUtil.toJsonStr(bean));
    
    
            //YAMLMapper yamlMapper = new YAMLMapper();
            //String content = yamlMapper.writeValueAsString(templateBean);
            //System.out.println(content);
            //
            //File f=new File("D:\\Java-develop\\test\\template.yaml");
            //FileOutputStream fileOutputStream = new FileOutputStream(f);
            //PrintStream printStream = new PrintStream(fileOutputStream);
            //System.setOut(printStream);
            //System.out.println(content);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    DumperOptions和YAMLMapper 这里两个生成的yml都是可用的,亲测都没有问题

    2.生成的yml文件如下

    !!com.example.test.bean.TemplateBean
    clientSettings:
      channelBufferSize: 128
      concurrency: 10
      connection:
        address: 127.0.0.1:9669
        password: nebula
        user: root
      postStart: null
      preStop: null
      retry: 3
      space: dataImport
    description: web console import
    files:
    - batchSize: 60
      csv:
        delimiter: null
        withHeader: false
        withLabel: false
      failDataPath: /data1/opt/itemFail.csv
      inOrder: null
      limit: null
      path: /data1/opt/item.csv
      schema:
        edge: null
        type: vertex
        vertex:
          tags:
          - name: item
            props:
            - index: 0
              name: id_single_item
              type: string
            - index: 0
              name: id_single_item
              type: string
            - index: 0
              name: id_single_item
              type: string
          vid:
            function: null
            index: 0
            prefix: null
            type: string
      type: csv
    logPath: /data1/opt/import.log
    removeTempFiles: null
    version: v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    总结

    这样我就实现了,每次通过nebula导入的工具导入的时候,都可以实现动态的yml生成;
    通过这种方式,就动态生成了yml配置文件,可以以此作为参考,后续可以根据这种方式做其他事情;
    例如: 服务器上动态生成yml配置文件,防止信息丢失

  • 相关阅读:
    SpringBoot过滤器和拦截器
    深入浅出理解串口
    Inksape 设置画布像素尺寸及透明背景
    基于SSH开发酒店管理系统
    史上最简SLAM零基础解读(7) - g2o(图优化)→简介环境搭建(slam十四讲为例)
    基于HTTP2/3的流模式消息交换如何实现?
    车路协同自动驾驶研究
    知识点记录:李群李代数,微分流形,微分几何,图论
    python matplot画图攻略
    Oracle表空间、用户详解
  • 原文地址:https://blog.csdn.net/qq_32419139/article/details/126875409