在Java util中获取hadoop配置

use*_*284 9 hadoop hdfs

我正在编写一个需要访问DFS的Java实用程序,所以我需要一个Configuration对象.当我简单地使用创建一个

Configuration conf = new Configuration()

它似乎没有找到DFS,只使用本地文件系统; 印花

fs.getHomeDirectory()

给我本地的主目录.我已经尝试将core-site.xml,mapred-site.xml,yarn-site.xml和hdfs-site.xml添加到Configuration作为资源,但它不会更改任何内容.我需要做些什么来获取HDFS设置?

谢谢阅读

Sac*_*inJ 18

它指向您的本地文件系统的原因是core-site.xml并且hdfs-site.xml没有正确添加.下面的代码片段可以帮助您.

Configuration conf = new Configuration();
conf.addResource(new Path("file:///etc/hadoop/conf/core-site.xml")); // Replace with actual path
conf.addResource(new Path("file:///etc/hadoop/conf/hdfs-site.xml")); // Replace with actual path

Path pt = new Path("."); // HDFS Path
FileSystem fs = pt.getFileSystem(conf);

System.out.println("Home directory :"+fs.getHomeDirectory());
Run Code Online (Sandbox Code Playgroud)

更新:

上面的选项应该工作,似乎配置文件或路径中的一些问题.您有另一个选项,而不是使用addResource方法添加配置文件,使用set方法.打开您的core-site.xml文件并找到值fs.defaultFS.使用set方法而不是addResource方法.

conf.set("fs.defaultFS","hdfs://<Namenode-Host>:<Port>");  // Refer you core-site.xml file and replace <Namenode-Host> and <Port> with your cluster namenode and Port (default port number should be `8020`). 
Run Code Online (Sandbox Code Playgroud)