Java对象转换成yaml文件

如何将Java代码转换成Yaml文件

目标yaml文件:

groups:
- name: Default
  rules:
  - alert: 自定义CPU使用率超限
    annotations: {summary: TestSummay}
    expr: rate(container_cpu_usage_seconds_total{container_name!="POD",id!="/",image!="",namespace!=""}[1m])>0.2
    for: 5m
    labels: {severity: critical}

PromRules类:

import java.util.ArrayList;
import java.util.List;

public class PromRules {

	private List groups = new ArrayList();

	public List getGroups() {
		return groups;
	}

	public void setGroups(List groups) {
		this.groups = groups;
	}
}

Group类:

import java.util.ArrayList;
import java.util.List;

public class Group {

	private String name;
	
	private List rules = new ArrayList();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List getRules() {
		return rules;
	}

	public void setRules(List rules) {
		this.rules = rules;
	}
}

Rule类:

public class Rule {
	
    private String alert;
	
	private String expr;
			
	private Label labels;
	
	private Annotation annotations;
	
	private String forTime;

	
	public String getFor() {
		return forTime;
	}

	public void setFor(String forTime) {
		this.forTime = forTime;
	}
	
	public String getAlert() {
		return alert;
	}

	public void setAlert(String alert) {
		this.alert = alert;
	}

	public String getExpr() {
		return expr;
	}

	public void setExpr(String expr) {
		this.expr = expr;
	}

	public Label getLabels() {
		return labels;
	}

	public void setLabels(Label labels) {
		this.labels = labels;
	}

	public Annotation getAnnotations() {
		return annotations;
	}

	public void setAnnotations(Annotation annotations) {
		this.annotations = annotations;
	}
}

注:yaml文件中的 for: 5mfor对应着getFor、setFor,而不是private String for;。当然了,由于for是关键字,我们也不可以将他作为一个变量名。

Label类:

public class Label {

	private String severity;

	public String getSeverity() {
		return severity;
	}

	public void setSeverity(String severity) {
		this.severity = severity;
	}
}

Annotation类:

public class Annotation {

	private  String summary;

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}
}

Test实现类:

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.yaml.snakeyaml.Yaml;

PromRules pr=new PromRules();
List groups = new ArrayList();
Group group = new Group();			
group.setName(groupName);		
		
List rules = new ArrayList();          
Rule rule = new Rule();
rule.setAlert(alertName);          
rule.setExpr(expr);
rule.setFor(forTime);
Label label = new Label();
label.setSeverity(severity);
rule.setLabels(label);
Annotation annotation = new Annotation();
annotation.setSummary(summary);
rule.setAnnotations(annotation);
rules.add(rule);  
		         	
group.setRules(rules);
groups.add(group);
pr.setGroups(groups);
Yaml yaml = new Yaml();
String generatePath="****";
yaml.dump(pr, new FileWriter(generatePath));

根据yaml文件缩进表示层级关系可知,这个yaml文件分成三层。
第一层显然是个List。

groups:

第二层一个String,一个List。

name: Default
rules:

第三层:

alert: 自定义CPU使用率超限
annotations: {summary: TestSummay}
expr: rate(container_cpu_usage_seconds_total{container_name!="POD",id!="/",image!="",namespace!=""}[1m])>0.2
for: 5m
labels: {severity: critical}

读取解析Yaml文件

Yaml yamlRead = new Yaml();
PromRules pr = yamlRead.loadAs(new FileInputStream(new File("***\\testRules.yml")), PromRules.class);

将某一路径下的yaml文件解析成PromRules对象。

你可能感兴趣的:(常用代码)