Hadoop配置属性返回Null
问题描述:
我写了一个简单的代码来测试如何在Hadoop中设置配置。Hadoop配置属性返回Null
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>
答
资源需要添加一个网址,否则字符串被解释为一个类路径资源(这在目前不能解决,将被忽略 - 我知道你,你认为一个警告消息将被倾倒的地方):
/**
* 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"));
}
感谢您的回答。实际上'conf.addResource(新路径(“”))'也适用。 – abhinavkulkarni 2012-07-13 22:40:08