结论:
参数从高到低:代码中指定的>项目中配置文件中指定的>Hadoop环境中用户指定的>default
示例1:测试Hadoop环境中用户指定的什么配置文件都不需要,只需要如下代码:
public static void main(String[] args) throws Exception {
Configuration cfg = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hcmaster:9000"),cfg);
fs.copyFromLocalFile(new Path("E:/test/core-site.xml"), new Path("/a.xml"));
fs.close();
}
结果:
在项目的类路径下面创建文件hdfs-site.xml,添加如下所示的内容:
dfs.replication
1
上传文件的代码:
public static void main(String[] args) throws Exception {
Configuration cfg = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hcmaster:9000"),cfg);
fs.copyFromLocalFile(new Path("E:/test/core-site.xml"), new Path("/a.xml"));
fs.close();
}
结果:
保留示例2中类路径下面创建文件hdfs-site.xml,修改测试代码:
public static void main(String[] args) throws Exception {
Configuration cfg = new Configuration();
cfg.set("dfs.replication","2");
FileSystem fs = FileSystem.get(new URI("hdfs://hcmaster:9000"),cfg);
fs.copyFromLocalFile(new Path("E:/test/core-site.xml"), new Path("/a.xml"));
fs.close();
}
结果: