[英]Hadoop Configuration object not pointing to hdfs file system
Hi I am trying to create small Spark program in Java. 嗨,我正在尝试用Java创建小型Spark程序。 I am creating the hadoop configuration object as show below: 我正在创建hadoop配置对象,如下所示:
Configuration conf = new Configuration(false);
conf.addResource(new Path("/dir/core-site.xml"));
conf.addResource(new Path("/dir/hdfs-site.xml"));
conf.addResource(new Path("/dir/yarn-site.xml"));
When I call the following I get file system but it points to local file system and not the hdfs file system 当我调用以下命令时,我得到文件系统,但它指向本地文件系统,而不是hdfs文件系统
FileSystem fs = FileSystem.get(conf);
Please guide I am new to Hadoop. 请指导我是Hadoop的新手。 Thanks in advance. 提前致谢。
I usually keep the core-site.xml
(along with the other files) in the classpath and get the configuration as follows: 我通常将core-site.xml
(以及其他文件)保留在类路径中,并按如下所示进行配置:
Configuration conf = new Configuration();
FileSystem localfs = FileSystem.getLocal(conf);
FileSystem hdfs = FileSystem.get(conf);
Here is the content of my core-site.xml
: 这是我的core-site.xml
:
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://{hadoop-server-addr}</value>
</property>
</configuration>
Add your hdfs-site.xml and core-site.xml to class path. 将您的hdfs-site.xml和core-site.xml添加到类路径。 And use following code. 并使用以下代码。
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.newInstance(conf);
It will pick the configs mentioned in the xmls. 它将选择xml中提到的配置。
To debug the issue better, add this line after creating a new Configuration instance. 为了更好地调试问题,请在创建新的Configuration实例后添加此行。 This will give you a better idea. 这会给你一个更好的主意。
Configuration conf = new Configuration(false);
conf.setQuietMode(false);
In most cases, defaulting to local filesystem instead of hdfs occurs when Configuration
is not able to load resources specified by the Path strings. 在大多数情况下,当Configuration
无法加载由路径字符串指定的资源时,默认为本地文件系统而不是hdfs。 In such cases, the default behaviour of Configuration
is to fail quietly and return null
for requested key. 在这种情况下, Configuration
的默认行为是静默失败并为请求的密钥返回null
。 In this case, the requested key is fs.defaultFS
, the namenode host. 在这种情况下,请求的密钥是fs.defaultFS
,即namenode主机。 FileSystem
instance then defaults to the file:///
scheme which is the local file system. 然后, FileSystem
实例默认为file:///
方案,这是本地文件系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.