Hadoop配置属性返回Null [英] Hadoop Configuration Property Returns Null

查看:215
本文介绍了Hadoop配置属性返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的代码来测试如何在Hadoop中设置配置。

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

上述程序的输出是:

Configuration: core-default.xml, core-site.xml, ~/conf.xml
null

因此 conf.get(color)返回 null 。但是,我已经在 conf.xml 中明确设置了以下属性:

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>


推荐答案

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

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);
}

无论如何,试试这个(我在syserr中得到黄色):

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

这篇关于Hadoop配置属性返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆