代码之家  ›  专栏  ›  技术社区  ›  abhinavkulkarni

Hadoop配置属性返回Null

  •  1
  • abhinavkulkarni  · 技术社区  · 13 年前

    我写了一个简单的代码来测试如何在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>
    
    1 回复  |  直到 13 年前
        1
  •  3
  •   Chris White    13 年前

    该资源需要添加为URL,否则String将被解释为类路径资源(目前无法解析并被忽略-我知道你认为警告消息会被转储到某个地方):

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

    无论如何,试试这个(我在系统错误中得到黄色):

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