2012-07-13 4 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.

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

답변

3

자원이 URL로 추가해야합니다, 그렇지 않으면 문자열이 순간에 해결되지 않고 무시되는 클래스 패스 리소스 (로 해석됩니다 다음과 같이 그러나, 나는 명시 적으로 conf.xml에 해당 속성을 설정 한 - 나는) 당신이 경고 메시지가 어딘가에 덤프 될 것이라고 생각하는지 :

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

)이 (i는 시스템 오류 노란색 해보 : 대답에 대한

@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

관련 문제