使用 SnakeYAML Java 将用户添加到 yml 文件作为嵌套在块序列中的块映射

Adding user to yml file as Block mapping nested in a block sequence, using SnakeYAML Java

我正在使用 Elastic Search,我遇到了一个名为 ReadOnlyRest 的插件,用于 Auth 目的。要设置此插件,我们需要将用户添加到 Elastic search yml。

所以我搜索了如何使用 Java 将 "key : value" 对数据添加到 yml。遇到 SnakeYAML 添加数据。

我可以从 Java 发送用户数据。

Java代码。

package com.test.elasticsearch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;


public class YAMLSample {

protected static Logger logger = Logger.getLogger(YAMLSample.class);

final String fileName = "/home/Installation/elasticsearch-2.3.1/config/elasticsearch.yml";

    public void writeToYML() throws IOException {
       logger.debug("Write to YML");

       Map<String, Object> data = new HashMap<String, Object>();
       data.put("name", "user5");
       data.put("type", "allow");
       data.put("auth_key", "user5:user5");
       data.put("kibana_access", "ro");
       data.put("indices", new String[] { ".kibana*", "abc", "def" });

       List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
       list.add(data);

       DumperOptions options = new DumperOptions();
       options.setIndent(5);

       Yaml yaml = new Yaml(options);
       FileWriter writer = new FileWriter(fileName, true);

       yaml.dump(list, writer);
       logger.debug("DONE!");

    }

    public static void main(String[] args) throws IOException {
        // new YAMLSample().readYML();
        new YAMLSample().writeToYML();
    }
}

以上代码的输出是:

 -     name: user5
       indices: [.kibana*, abc, def]
       kibana_access: ro
       type: allow
       auth_key: user5:user5

但是,预期的输出是:

    - name: user5
      indices: [.kibana*, abc, def]
      kibana_access: ro
      type: allow
      auth_key: user5:user5

"Hyphen-minus" 应该只有一个 space 而在 "Hyphen-minus" 之前应该有 4 个 space。

我的意思是说我希望它显示为用户数组。除了 "Hyphen-minus" 之外,很少有 space。

请帮助我找出解决方案。

我修改了你的代码,得到了预期的结果。下面是代码的样子:

public class YAMLSample {

  final String fileName = "/tmp/rest.yml";

  public void writeToYML() throws IOException {
    log( "Write to YML" );

    Map<String, Object> user = new HashMap<>();
    user.put( "name", "user5" );
    user.put( "type", "allow" );
    user.put( "auth_key", "user5:user5" );
    user.put( "kibana_access", "ro" );
    user.put( "indices", new String[] { ".kibana*", "abc", "def" } );

    Map<String, Object> user2 = new HashMap<>();
    user2.put("name", "user2");
    user2.put("type", "allow");
    user2.put("auth_key", "user2:user2");
    user2.put("kibana_access", "ro");
    user2.put("indices", new String[] { ".kibana*", "abc", "def" });

    List<Map<String, Object>> list = new ArrayList<>();
    list.add(user);
    list.add(user2);

    Map<String, List<Map<String, Object>>> config = new HashMap<>();
    config.put( "access_control_rules", list );

    DumperOptions options = new DumperOptions();
    options.setIndent( 6 );
    options.setIndicatorIndent( 4 );
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);

    Yaml yaml = new Yaml(options);
    FileWriter writer = new FileWriter(fileName, true);

    yaml.dump( config, writer );
    log( "DONE!" );
  }

  public static void main(String[] args) throws IOException {
    new YAMLSample().writeToYML();
  }

  public void log(String str) {
    System.out.println(str);
  }
}

基本上我将这两个选项添加到您的 Dumper
options.setIndicatorIndent(4); options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);

并从 5 更新到 6 options.setIndent(6);