1

我的以下代码不会产生预期的输出:

public static void main(String[] args) throws MalformedURLException {

    Configuration.addDefaultResource("/home/some_user/conf.xml");
    Configuration conf = new Configuration();
    System.out.println(conf);
    System.out.println(conf.get("color"));
    assertThat(conf.get("color"), is("yellow"));
}

该属性color在 conf.xml 文件中设置如下:

<property>
        <name>color</name>
        <value>yellow</value>
        <description>Color</description>
</property>

看起来文件conf.xml没有被合并到默认配置中。

的文档Configuration.addDefaultResource(String param)说参数应该在类路径中。当我已经为程序提供完整的绝对路径时,我不明白如何将文件添加到类路径中。

4

1 回答 1

2

第一个观察:我不知道您使用哪个版本的 hadoop,但 addDefaultResource() 已被弃用很长时间。

在更高版本的 Hadoop 中,完成您想要的标准方法是:

Configuration conf = new Configuration()
conf.addResource("path/to/file");
...

Regarding the classpath issue, you have to simply place the config file in the classpath. So you have to discover what the classpath is to(it is either an environment var or the one which you set with -classpath option). If you didn't use the -classpath option and there is no classpath environment variable then it is automatically set to the current directory (".")

于 2012-07-13T23:30:37.250 回答