gpt4 book ai didi

configuration - Hadoop 配置属性返回 Null

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

我写了一个简单的代码来测试如何在 Hadoop 中设置配置。

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。但是,我已在 conf.xml 中明确设置该属性,如下所示:

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

最佳答案

资源需要作为 URL 添加,否则字符串将被解释为类路径资源(目前无法解析并被忽略 - 我知道你认为警告消息会被转储到某处):

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

无论如何,试试这个(我在 syserr 中变黄):

@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"));
}

关于configuration - Hadoop 配置属性返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478036/

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