hadoop core-site.xml 中通常有核心的配置项,比如:
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/Users/user1/software_install/hadoop_install/hadoop_tmp_dir</value>
</property>
<property>
<name>ha.zookeeper.quorum</name>
<value>localhost:2181</value>
</property>
<property>
<name>hive.zookeeper.quorum</name>
<value>localhost:2181</value>
</property>
</configuration>
如图,当resources目录下添加了Hadoop core-site.xml 且包含配置项“fs.defaultFS”
时,应用代码中不需要
显式指定 Hadoop 配置项 “fs.defaultFS”;
但是如果resources目录未添加该配置文件或者不包含该配置项,则必须要在应用代码中显式指定该配置项,否则当使用相对路径访问HDFS文件时:
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(new Path("/user/hive/warehouse/test.db/tbl1/000000_0")
// 完整路径应该为:hdfs://localhost:9000/user/hive/warehouse/test.db/tbl1,其中hdfs://localhost:9000为配置项“fs.defaultFS”的值。
会提示找不到该文件:
java.io.FileNotFoundException: File /user/hive/warehouse/test.db/tbl1 does not exist