简体   繁体   English

Apache Storm-从Config中读取YAML列表

[英]Apache Storm - read in YAML list from Config

I am writing a Apache Storm topology which takes in a configuration file on the command line. 我正在编写一个Apache Storm拓扑,该拓扑在命令行中包含一个配置文件。 The configuration file is YAML and looks like this 配置文件是YAML,看起来像这样

#Ports
ports:
  - 9998
  - 9997
  - 9996

I load the configuration file into the Storm config using snakeyaml and the following code, resource is set to the string of the path of the configuration yaml file: 我使用snakeyaml和以下代码将配置文件加载到Storm配置中,资源设置为配置yaml文件路径的字符串:

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

I have verified that the Config object now contains the list of ports. 我已验证Config对象现在包含端口列表。 If I print the the values of the "ports" it looks to be the following "[9998, 9997, 9996]". 如果我打印“端口”的值,它将显示为以下的“ [9998,9997,9996]”。

My question is how to I get this into an Array, ArrayList, or any data structure I can use in Java? 我的问题是如何将其放入Array,ArrayList或可以在Java中使用的任何数据结构中? I have been using the backtype.storm.utils.Utils class which has a helper method get, but it only works when I have a single value not a list of values. 我一直在使用backtype.storm.utils.Utils类,该类具有一个辅助方法get,但是它仅在我具有单个值而不是值列表时才起作用。

I have tried calling it like so: 我试过这样称呼它:

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

but, I get a casting exception. 但是,我得到一个强制转换异常。 I have also tried: 我也尝试过:

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

to check to see if I can just get the entire string and that also has a cast exception. 检查我是否只能获取整个字符串,并且还具有强制转换异常。 I am not sure what the Config object is storing this list as. 我不确定Config对象将此列表存储为什么。 A Map? 一张地图?

Any help would be appreciated. 任何帮助,将不胜感激。

I would recommend using Jackson's YAML module ( https://github.com/FasterXML/jackson-dataformat-yaml ); 我建议使用Jackson的YAML模块( https://github.com/FasterXML/jackson-dataformat-yaml ); this is what many frameworks like DropWizard do. 这就是DropWizard等许多框架所做的。 If so, you can use data-binding exactly as with JSON, to construct POJOs for configuration access: 如果是这样,则可以像使用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

I am not sure what the answer is, but I ended up updating my snakeyaml version to 1.14 and I am now using the yaml.loadAs() method to directly load the cotents of the yaml file into a pojo. 我不确定答案是什么,但是我最终将snakeyaml版本更新为1.14,现在我使用yaml.loadAs()方法将yaml文件的内容直接加载到pojo中。 From there I am able to get what I need. 从那里我可以得到所需的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM