1

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

1 回答 1

3

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

/**
 * 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"));
}
于 2012-07-13T22:02:34.237 回答