在 Java util 中获取 hadoop 配置
Get hadoop configuration in Java util
我正在编写一个需要访问 DFS 的 Java 实用程序,因此我需要一个 Configuration
对象。
当我简单地使用
创建一个时
Configuration conf = new Configuration()
好像没有找到DFS,直接使用本地文件系统;打印
fs.getHomeDirectory()
提供我的本地主目录。我试过添加
core-site.xml,mapred-site.xml,yarn-site.xml, and hdfs-site.xml 作为资源添加到配置中,但它没有改变任何东西。我需要做什么才能让它获取 HDFS 设置?
感谢阅读
要访问文件系统,您必须使用下面概述的配置和文件系统
- 获取配置实例
获取HDFS实例
Configuration configuration = new Configuration();
FileSystem hdfs = FileSystem.get(new URI("hdfs://"+HadoopLocation+":8020"), configuration);
在这种情况下,HadoopLocation 是您拥有 hadoop 服务器的位置(可能是本地主机)
它指向您的本地文件系统的原因是 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());
更新:
以上选项应该有效,配置文件或路径中似乎存在一些问题。您有另一种选择,而不是使用 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`).
我正在编写一个需要访问 DFS 的 Java 实用程序,因此我需要一个 Configuration
对象。
当我简单地使用
Configuration conf = new Configuration()
好像没有找到DFS,直接使用本地文件系统;打印
fs.getHomeDirectory()
提供我的本地主目录。我试过添加 core-site.xml,mapred-site.xml,yarn-site.xml, and hdfs-site.xml 作为资源添加到配置中,但它没有改变任何东西。我需要做什么才能让它获取 HDFS 设置?
感谢阅读
要访问文件系统,您必须使用下面概述的配置和文件系统
- 获取配置实例
获取HDFS实例
Configuration configuration = new Configuration(); FileSystem hdfs = FileSystem.get(new URI("hdfs://"+HadoopLocation+":8020"), configuration);
在这种情况下,HadoopLocation 是您拥有 hadoop 服务器的位置(可能是本地主机)
它指向您的本地文件系统的原因是 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());
更新:
以上选项应该有效,配置文件或路径中似乎存在一些问题。您有另一种选择,而不是使用 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`).