需求:
Java 操作,通过标准的json格式,生成yaml文件,然后再读取yaml文件修改里面的某(多)个变量值(gte,lte,interval,min,max),然后再生成新的json,json格式前后都一样,就是为了修改里面的某些变量值,所以用yaml作为中间桥梁

通过yaml生成的json格式:

{
    "size": 0,
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "last_updated": { "gte": 1497283200000, "lte": 1497928996980 } }
            }
        }
    },
    "aggs": {
        "by_time": {
            "date_histogram": {
                "field": "last_updated",
                "interval": "21526566ms",
                "extended_bounds": {
                    "min": "1497283200000",
                    "max": "1497928996980" }
            },
            "aggs": {
                "event_status_group": {
                    "filters": { "filters": { "info": { "match_phrase": { "alert_status": "info" } }, "warning": { "match_phrase": { "alert_status": "warning" } }, "error": { "match_phrase": { "alert_status": "error" } }, "success": { "match_phrase": { "alert_status": "success" } } } } }
            }
        }
    }
}

生成的yaml格式:

size: 0
query:
  constant_score:
    filter:
      range:
        last_updated: {gte: 1497283200000, lte: 1497928996980}
aggs:
  by_time:
    date_histogram:
      field: last_updated
      interval: 21526566ms
      extended_bounds: {min: '1497283200000', max: '1497928996980'}
    aggs:
      event_status_group:
        filters:
          filters:
            info:
              match_phrase: {alert_status: info}
            warning:
              match_phrase: {alert_status: warning}
            error:
              match_phrase: {alert_status: error}
            success:
              match_phrase: {alert_status: success}

在控制台打印出来的yaml格式是这种,但是写入到yaml文件的格式是上面那种(可能是语法问题),但是两种yaml格式生成的json都是一样的

size: 0
query:
  constant_score:
    filter:
      range:
        last_updated:
          gte: 1234567899999
          lte: 1111111111111
aggs:
  by_time:
    date_histogram:
      field: "last_updated"
      interval: "21526566ms"
      extended_bounds:
        min: "1497283200000"
        max: "1497928996980"
    aggs:
      event_status_group:
        filters:
          filters:
            info:
              match_phrase:
                alert_status: "info"
            warning:
              match_phrase:
                alert_status: "warning"
            error:
              match_phrase:
                alert_status: "error"
            success:
              match_phrase:
                alert_status: "success"

工具类和main函数

package com.mdl.yaml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.google.gson.Gson;
import com.mdl.yaml.bean.Aggs;
import com.mdl.yaml.bean.Query;

public class TestYaml {

    public static void main(String[] args) {

//      readJsonAndCreateYaml("conf/param.json","conf/patam123.yaml");

//      yamlToJson("conf/file.yaml");

        String result = getJsonParam("conf/file.yaml", 111111, 22222222, "333333", "4444444", "1000");

        System.out.println("\n" + "所有数据修改之后--1" + "\n" + result);

    }


    /**
     * 獲取并設置yaml的值
     * 
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static String getJsonParam(String yaml_url, long lte, long gte, String max, String min, String interval) {
        Map map = returnMapFromYaml(yaml_url);

        Gson gs = new Gson();
        // 设置最后一次更新的起止时间
        String queryStr = gs.toJson((Map) map.get("query"));
        // size的默认为0
        map.put("size", 0);
        Query q = (Query) gs.fromJson(queryStr, Query.class);
        q.getConstant_score().getFilter().getRange().getLast_updated().setLte(lte);
        q.getConstant_score().getFilter().getRange().getLast_updated().setGte(gte);
        ((Map) ((Map) map).get("query")).put("constant_score", q.getConstant_score());

        // 设置bounds的最大最小时间
        String aggstr = gs.toJson((Map) map.get("aggs"));
        Aggs a = (Aggs) gs.fromJson(aggstr, Aggs.class);
        a.getBy_time().getDate_histogram().getExtended_bounds().setMax(max);
        a.getBy_time().getDate_histogram().getExtended_bounds().setMin(min);
        a.getBy_time().getDate_histogram().setInterval(interval);
        ((Map) ((Map) ((Map) map).get("aggs")).get("by_time")).put("date_histogram",
                a.getBy_time().getDate_histogram());

        return gs.toJson(map);
    }

    /**
     * 讀取json并生成yaml
     */
    public static void readJsonAndCreateYaml(String json_url,String yaml_url) {
        try {
            String param = readJson(json_url);
            createYaml(yaml_url,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 將json轉化為yaml格式并生成yaml文件
     * @param jsonString
     * @return
     * @throws JsonProcessingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static void createYaml(String yaml_url,String jsonString) throws JsonProcessingException, IOException {
        // parse JSON
        JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
        // save it as YAML
        String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);

        Yaml yaml = new Yaml();
        Map<String,Object> map = (Map<String, Object>) yaml.load(jsonAsYaml);

        createYamlFile(yaml_url, map);
    }

    /**
     * 将数据写入yaml文件
     * @param url yaml文件路径
     * @param data 需要写入的数据
     */
    public static void createYamlFile(String url,Map<String, Object> data){
        Yaml yaml = new Yaml();
        FileWriter writer;
        try {
            writer = new FileWriter(url);
            yaml.dump(data, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 讀取json文件并返回字符串
     * 
     * @param url
     * @return
     * @throws Exception
     */
    @SuppressWarnings("resource")
    public static String readJson(String url) throws Exception {
        File file = new File(url);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufReader = new BufferedReader(fileReader);
        String message = new String();
        String line = null;
        while ((line = bufReader.readLine()) != null) {
            message += line;
        }
        return message;
    }

    /**
     * 方法一
     * 
     * 讀取yaml生成Map并返回
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> yamlToMap(String yaml_url) {
        Map<String, Object> loaded = null;
        try {
            FileInputStream fis = new FileInputStream(yaml_url);
            Yaml yaml = new Yaml();
            loaded = (Map<String, Object>) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return loaded;
    }

    /**
     * 方法二
     * 
     * 读取yaml的内容并转为map
     * @param url
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map returnMapFromYaml(String yaml_url) {
        YamlReader reader;
        Object object = null;
        Map map = null;
        try {
            reader = new YamlReader(new FileReader(yaml_url));
            object = reader.read();
            map = (Map) object;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (YamlException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 讀取yaml生成json并返回
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String yamlToJson(String file) {
        Gson gs = new Gson();
        Map<String, Object> loaded = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            Yaml yaml = new Yaml();
            loaded = (Map<String, Object>) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return gs.toJson(loaded);
    }



//  /**
//   * demo
//   * 生成yaml格式的string,并寫入yaml文件
//   */
//  public static void testDumpWriterforyaml() {
//      Map<String, Object> data = new HashMap<String, Object>();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//
//      Yaml yaml = new Yaml();
//      FileWriter writer;
//      try {
//          writer = new FileWriter("conf/param123.yaml");
//          yaml.dump(data, writer);
//      } catch (IOException e) {
//          e.printStackTrace();
//      }
//  }
//
//  /**
//   * 打印生成yaml格式的string
//   */
//  public static void testDump() {
//      Map<String, Object> data = new HashMap<String, Object>();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//      Yaml yaml = new Yaml();
//      String output = yaml.dump(data);
//      System.out.println(output);
//  }
//  
//  /**
//   * 打印生成yaml格式的string
//   */
//  public static void printDumpWriter() {
//      Map<String, Object> data = new HashMap<String, Object>();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//      Yaml yaml = new Yaml();
//      StringWriter writer = new StringWriter();
//      yaml.dump(data, writer);
//      System.out.println(writer.toString());
//  }
}

Aggs.java的实体

package com.mdl.yaml.bean;

public class Aggs {

    private ByTime by_time;

    public ByTime getBy_time() {
        return by_time;
    }

    public void setBy_time(ByTime by_time) {
        this.by_time = by_time;
    }


    public class ByTime{
        private DateHistogram date_histogram;

        public DateHistogram getDate_histogram() {
            return date_histogram;
        }

        public void setDate_histogram(DateHistogram date_histogram) {
            this.date_histogram = date_histogram;
        }

    }


    public class DateHistogram {
        private String field;
        private String interval;
        private ExtendedBounds extended_bounds;

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getInterval() {
            return interval;
        }

        public void setInterval(String interval) {
            this.interval = interval;
        }

        public ExtendedBounds getExtended_bounds() {
            return extended_bounds;
        }

        public void setExtended_bounds(ExtendedBounds extended_bounds) {
            this.extended_bounds = extended_bounds;
        }

    }

    public class ExtendedBounds{
        private String min;
        private String max;

        public String getMin() {
            return min;
        }
        public void setMin(String min) {
            this.min = min;
        }
        public String getMax() {
            return max;
        }
        public void setMax(String max) {
            this.max = max;
        }
    }


}

Query.java的实体

package com.mdl.yaml.bean;

public class Query {

    private ConstantScore constant_score;

    public ConstantScore getConstant_score() {
        return constant_score;
    }

    public void setConstant_score(ConstantScore constant_score) {
        this.constant_score = constant_score;
    }


    public class ConstantScore{
        private Filter filter;

        public Filter getFilter() {
            return filter;
        }

        public void setFilter(Filter filter) {
            this.filter = filter;
        }
    }

    public class Filter{
        private Range range;

        public Range getRange() {
            return range;
        }

        public void setRange(Range range) {
            this.range = range;
        }
    }

    public class Range{
        private LastUpdated last_updated;

        public LastUpdated getLast_updated() {
            return last_updated;
        }

        public void setLast_updated(LastUpdated last_updated) {
            this.last_updated = last_updated;
        }
    }

    public class LastUpdated{
        private long gte;
        private long lte;
        public long getGte() {
            return gte;
        }
        public void setGte(long gte) {
            this.gte = gte;
        }
        public long getLte() {
            return lte;
        }
        public void setLte(long lte) {
            this.lte = lte;
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.ele</groupId>
    <artifactId>elastic.util.giraffe</artifactId>
    <version>1.0.9-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>elastic.util.giraffe</name>

    <properties>
        <!-- Java -->
        <java-version>1.8</java-version>
        <!-- ElasticSearch -->
        <elasticsearch-version>2.3.0</elasticsearch-version>
        <!-- <elasticsearch-version>5.3.1</elasticsearch-version> -->
        <!-- Spring -->
        <spring-framework.version>4.2.1.RELEASE</spring-framework.version>
    </properties>

    <dependencies>
        <dependency>
             <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch-version}</version>
        </dependency>
        <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>1.7.5</version>
        </dependency>
        <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-simple</artifactId>
           <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.18</version>
        </dependency>

        <dependency>  
            <groupId>com.esotericsoftware.yamlbeans</groupId>  
            <artifactId>yamlbeans</artifactId>  
            <version>1.08</version>  
        </dependency>  


        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <!-- json -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.9.0.pr4</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <compilerArguments>
                        <parameters/>
                    </compilerArguments>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>

        </plugins>
    </build>



    <distributionManagement>
        <repository>
            <id>ele-bpm-release</id>
            <url>http://maven.dev.elenet.me/nexus/content/repositories/ele-bpm-release/</url>
        </repository>
        <snapshotRepository>
            <id>ele-bpm-snapshots</id>
            <url>http://maven.dev.elenet.me/nexus/content/repositories/ele-bpm-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>


</project>
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐