【问题标题】:Apache Storm - read in YAML list from ConfigApache Storm - 从 Config 中读取 YAML 列表
【发布时间】:2014-12-17 17:07:01
【问题描述】:

我正在编写一个 Apache Storm 拓扑,它在命令行中接收一个配置文件。配置文件是YAML,看起来像这样

#Ports
ports:
  - 9998
  - 9997
  - 9996

我使用snakeyaml和以下代码将配置文件加载到Storm config中,resource设置为配置yaml文件的路径字符串:

Config conf = new Config();
Yaml yaml = new Yaml()
Map rs = (Map) yaml.load(new InputStreamReader(new FileInputStream(resource)));
conf.putAll(rs);

我已验证 Config 对象现在包含端口列表。如果我打印“端口”的值,它看起来是以下“[9998, 9997, 9996]”。

我的问题是如何将它放入 Array、ArrayList 或任何我可以在 Java 中使用的数据结构中?我一直在使用具有辅助方法 get 的 backtype.storm.utils.Utils 类,但它仅在我有单个值而不是值列表时才有效。

我试过这样称呼它:

 String[] ports = Utils.get(conf, "mimes", null); 

但是,我得到一个强制转换异常。我也试过:

 String ports = Utils.get(conf, "mimes", null); 

检查我是否可以获取整个字符串并且还有一个强制转换异常。我不确定 Config 对象将此列表存储为什么。一张地图?

任何帮助将不胜感激。

【问题讨论】:

    标签: java yaml apache-storm


    【解决方案1】:

    我会推荐使用 Jackson 的 YAML 模块 (https://github.com/FasterXML/jackson-dataformat-yaml);这就是像 DropWizard 这样的许多框架所做的。如果是这样,您可以像使用 JSON 一样使用数据绑定来构造 POJO 以进行配置访问:

    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    Config config = mapper.readValue(fileOrInputStream, Config.class);
    int port = mapper.server.port; // or getters, or however your Config object is laid out
    

    【讨论】:

      【解决方案2】:

      我不确定答案是什么,但我最终将我的 snakeyaml 版本更新到 1.14,我现在正在使用 yaml.loadAs() 方法将 yaml 文件的内容直接加载到 pojo 中。从那里我可以得到我需要的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-19
        • 2021-04-25
        • 2023-03-21
        • 2019-07-30
        • 2013-11-15
        • 2017-06-28
        • 2023-03-03
        • 2011-07-20
        相关资源
        最近更新 更多