Hadoop配置属性返回null(Hadoop Configuration Property Ret

2019-07-30 10:33发布

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

Answer 1:

资源需要添加一个网址,否则字符串被解释为一个类路径资源(这在目前不能解决,将被忽略 - 我知道你,你认为一个警告消息将被倾倒的地方):

/**
 * 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"));
}


文章来源: Hadoop Configuration Property Returns Null