gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:57 25 4
gpt4 key购买 nike

我正在使用 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”应该只有一个空格,在“Hyphen-minus”之前应该有 4 个空格。

我的意思是说我希望它显示为用户数组。除了“Hyphen-minus”之外还有几个空格。

请帮助我找出解决方案。

最佳答案

我已经修改了您的代码并得到了预期的结果。下面是代码的样子:

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);

并将 options.setIndent(6);

从 5 更新为 6

关于java - 使用 SnakeYAML Java 将用户添加到 yml 文件作为嵌套在 block 序列中的 block 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com