问题
I wrote a simple code to test how to set configuration in 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"));
}
The output of above program is:
Configuration: core-default.xml, core-site.xml, ~/conf.xml
null
Thus conf.get("color")
returns null
. However, I have explicitly set that property in conf.xml
as follows:
<property>
<name>color</name>
<value>yellow</value>
<description>Color</description>
</property>
回答1:
The resource needs to be added as a URL, otherwise the String is interpreted as a classpath resource (which at the moment does not resolve and is ignored - i know you you think that a warn message would be dumped somewhere):
/**
* 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);
}
Anyway, try this (i get yellow in the 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"));
}
来源:https://stackoverflow.com/questions/11478036/hadoop-configuration-property-returns-null