Configuration Hadoop配置属性返回Null

Configuration Hadoop配置属性返回Null,configuration,hadoop,configuration-files,Configuration,Hadoop,Configuration Files,我编写了一个简单的代码来测试如何在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")); } 上述程序的

我编写了一个简单的代码来测试如何在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>

颜色
黄色的
颜色

需要将资源添加为URL,否则该字符串将被解释为类路径资源(目前无法解析且被忽略-我知道您认为警告消息会转储到某个地方):


谢谢你的回答。实际上,
conf.addResource(新路径(“”)
也可以工作。
/**
 * 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);
}
@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"));
}