Hadoop Configuration Property Returns Null

烈酒焚心 提交于 2019-12-12 09:27:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!