目录创建
@Test
public void testMkdir() throws Exception {
//1 获取文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");
//2 上传文件
fs.mkdirs(new Path("/demo/test"));
//3 关闭资源
fs.close();
System.out.println("~ok~");
}
文件上传
@Test
public void testCopyFromLocalFile() throws Exception {
//1 获取文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");
//2 上传文件
fs.mkdirs(new Path("/demo/test"));
//3 关闭资源
fs.close();
System.out.println("~ok~");
}
参数优先级
在resource中添加配置文件hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
执行文件上传test
代码中添加配置
configuration.set("dfs.replication", "2");
@Test
public void testCopyFromLocalFile() throws Exception {
//1 获取文件系统
Configuration conf = new Configuration();
conf.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");
//2 上传文件
fs.copyFromLocalFile(new Path("/Users/ylj/demo/readme.md"),new Path("/demo/test/readme-3.md"));
//3 关闭资源
fs.close();
System.out.println("~ok~");
}
执行文件上传test
结论
参数优先级排序:
- 客户端代码中设置的值
- classpath下的用户自定义配置文件
- 服务器的默认配置