为什么system.setproperty不能在hadoop中更改配置属性?

wkyowqbh  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(405)

我的环境是ubuntu12.04+eclipse3.3.0+hadoop0.20.2
当我在system.serproperty上进行测试时,它将更改xml文件中定义的配置。但我在测试的时候没有得到同样的效果。下面是我的代码片段:

//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

谁能帮我一下吗?谢谢!

7bsow1i6

7bsow1i61#

这个 Configuration 对象未链接到系统属性-如果要更改 z 在配置中,然后使用 conf.set('z', 'SystemProp_mz') 而不是 System.setProperty(..) 更新
配置对象可以使用中概述的变量展开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");
}

相关问题