2012-07-13 115 views
1

我寫了一個簡單的代碼來測試如何在Hadoop中設置配置。Hadoop配置屬性返回Null

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> 

回答

3

資源需要添加一個網址,否則字符串被解釋爲一個類路徑資源(這在目前不能解決,將被忽略 - 我知道你,你認爲一個警告消息將被傾倒的地方):

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

感謝您的回答。實際上'conf.addResource(新路徑(「<絕對文件路徑>」))'也適用。 – abhinavkulkarni 2012-07-13 22:40:08