文章目录
- 使用场景
- 一、构造要生成yml的bean?
- 二、根据已有bean,设置值,然后生成yml文件
- 1.引入pom
- 2.生成的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<File> 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<Prop> 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<Tag> 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<Prop> 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";
}
}
可以看到,我这里面设置了一些默认值;
二、根据已有bean,设置值,然后生成yml文件
1.引入pom
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.0</version>
</dependency>
代码如下(示例):
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);
}
}
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
总结
这样我就实现了,每次通过nebula导入的工具导入的时候,都可以实现动态的yml生成;
通过这种方式,就动态生成了yml配置文件,可以以此作为参考,后续可以根据这种方式做其他事情;
例如: 服务器上动态生成yml配置文件,防止信息丢失