gpt4 book ai didi

java - 如何以编程方式为 hadoop *-site.xml 设置目录

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

我的 java 应用程序应该在运行时为 hadoop xml 定义文件夹(为简单起见,让它从程序命令行参数中获取)。我想,我可以使用类似下面的代码:

import org.apache.hadoop.conf.Configuration()

....

Configuration = new Configuration
config.addResource(new Path(String.format("%s/core-site.xml", hadoopLocation)));
config.addResource(new Path(String.format("%s/hdfs-site.xml", hadoopLocation)));
config.addResource(new Path(String.format("%s/tez-site.xml", hadoopLocation)));
config.addResource(new Path(String.format("%s/yarn-site.xml", hadoopLocation)));
config.addResource(new Path(String.format("%s/mapred-site.xml", hadoopLocation)));
config.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName() );
config.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName() );

try {
FileSystem dfs = FileSystem.get(config);
} catch (IOException e) {
LOGGER.error("Failed to process hadoop file system");
}

但是,我应该在我的程序中每次调用 new Configuration() 时重复它,或者使用一些持久的预配置 baseConfiguration 对象并像 new Configuration(基础配置)。所有这些方式看起来都很笨拙。有没有更优雅的解决方案。也许我可以执行一次类似静态方法调用的东西。以下代码仅供引用:

Configuration.setBaseXmlsDir();

最佳答案

Hadoop 应用程序在类路径中查找这些文件。如果您运行 hadoop classpath,您应该能够在列表中看到配置目录。

因此,要修改位置,您只需要在应用程序运行之前修改类路径即可。最简单的方法是在启动应用程序之前定义环境变量 HADOOP_CONF_DIR,如下所示:

export HADOOP_CONF_DIR=/your/path/to/xmls
hadoop jar ...

关于java - 如何以编程方式为 hadoop *-site.xml 设置目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228472/

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