我的环境是ubuntu12.04+eclipse3.3.0+hadoop0.20.2
当我对系统进行测试时。该属性将更改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
谁能给我一些帮助?非常感谢! Configuration
对象没有链接到System属性—如果您想在配置中更改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");
}