【问题标题】:Why System.setProperty cannot change the configuration attribute in Hadoop?为什么 System.setProperty 不能更改 Hadoop 中的配置属性?
【发布时间】:2013-06-18 08:40:02
【问题描述】:

我的环境是ubuntu12.04+eclipse3.3.0+hadoop0.20.2

当我在 System.serProperty 上进行测试时,它会更改 xml 文件中定义的配置。但是当我测试它时,我没有得到同样的效果。这是我的代码 sn-p:

//cofiguration class test
public static void test()   {
    Configuration conf = new Configuration();
    conf.addResource("raw/conf-1.xml");
    System.out.println(conf.get("z"));
    System.setProperty("z", "SystemProp_mz");
    System.out.println(conf.get("z"));
}

conf-1.xml如下:

<configuration>  
    <property>
        <name>z</name>
        <value>mz</value>
    </property>
</configuration>  

输出是:

mz
mz

谁能帮帮我?非常感谢!

【问题讨论】:

    标签: hadoop system-properties


    【解决方案1】:

    Configuration 对象未链接到系统属性 - 如果您想在配置中更改 z 的值,请使用 conf.set('z', 'SystemProp_mz') 而不是 System.setProperty(..)

    更新

    Configuration 对象可以使用http://hadoop.apache.org/docs/current/api/org/apache/hadoop/conf/Configuration.html 中所述的变量扩展,但这需要您定义如下条目:

    <configuration>  
      <property>
        <name>z</name>
        <value>${z}</value>
      </property>
    </configuration>
    

    如果您没有上述条目,则仅调用conf.get("z") 将不会解析为系统属性。下面的单元测试块演示了这一点:

    @Test
    public void testConfSystemProps() {
      System.setProperty("sysProp", "value");
      Configuration conf = new Configuration();
      conf.set("prop", "${sysProp}");
    
      Assert.assertNull(conf.get("sysProp"));
      Assert.assertEquals(conf.get("prop"), "value");
    }
    

    【讨论】:

    • 可以在Hadoop, the Definitive Guide 3rd中由System.setProperty设置。请参阅第 146 页(变量展开)
    • 确实如此,但以一种微妙的方式 - 答案已相应更新
    猜你喜欢
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多