我编写了一个简单的代码来测试如何在Hadoop中设置配置。
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.addResource("~/conf.xml");
System.out.println(conf);
System.out.println(conf.get("color"));
}
以上程序的输出是:
Configuration: core-default.xml, core-site.xml, ~/conf.xml
null
因此conf.get("color")
会返回null
。但是,我已在conf.xml
中明确设置了该属性,如下所示:
<property>
<name>color</name>
<value>yellow</value>
<description>Color</description>
</property>
答案 0 :(得分:3)
需要将资源添加为URL,否则将String解释为类路径资源(此时不会解析并被忽略 - 我知道您认为警告消息会被转储到某处):< / p>
/**
* Add a configuration resource.
*
* The properties of this resource will override properties of previously
* added resources, unless they were marked <a href="#Final">final</a>.
*
* @param name resource to be added, the classpath is examined for a file
* with that name.
*/
public void addResource(String name) {
addResourceObject(name);
}
无论如何,试试这个(我在syserr中变黄):
@Test
public void testConf() throws MalformedURLException {
Configuration conf = new Configuration();
conf.addResource(new File("~/conf.xml")
.getAbsoluteFile().toURI().toURL());
conf.reloadConfiguration();
System.err.println(conf);
System.err.println(conf.get("color"));
}