gpt4 book ai didi

hadoop - 为什么 System.setProperty 不能更改 Hadoop 中的配置属性?

转载 作者:可可西里 更新时间:2023-11-01 16:26:47 26 4
gpt4 key购买 nike

我的环境是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

谁能帮帮我?非常感谢!

最佳答案

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") 将不会解析为系统属性。以下单元测试 block 演示了这一点:

@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 - 为什么 System.setProperty 不能更改 Hadoop 中的配置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com