文章目录
一、HDFS文件上传
1. 编写源代码,在HdfsClient类中添加方法
@Test
public void testCopyFromLocalFile() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 0 配置文件的副本数
conf.set("dfs.replication", "2");
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 上传文件
fs.copyFromLocalFile(new Path("data/intr.txt"), new Path("/0202/mydir/intr.txt"));
// 3 关闭资源
fs.close();
System.out.println("over");
}
2. 将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>
3. 参数优先级
客户端代码中设置的值 >ClassPath
下的用户自定义配置文件 >服务器的默认配置
二、HDFS文件下载
@Test
public void testCopyToLocalFile() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 下载文件
// fs.copyToLocalFile(false, "","", false); 这种方式中第一个参数为是否删除源数据,最后一个参数表示是否使用本地文件系统,若使用会有一个.crc校验文件
fs.copyToLocalFile(new Path("/0202/mydir/zhuijia.txt"), new Path("data/intr_get.txt"));
// 3 关闭资源
fs.close();
System.out.println("over");
}
三、HDFS文件夹删除
@Test
public void testDelete() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 删除文件
fs.delete(new Path("/0202/mydir02"),true);
// 3 关闭资源
fs.close();
System.out.println("over");
}
四、HDFS文件名更改
@Test
public void testRename() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 修改文件名
fs.rename(new Path("/0202/mydir/intr.txt"),new Path("/0202/mydir/intr_rename.txt"));
// 3 关闭资源
fs.close();
System.out.println("over");
}
五、HDFS文件详情查看
@Test
public void testListFiles() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus status = listFiles.next();
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------分割线----------");
}
// 3 关闭资源
fs.close();
System.out.println("over");
}
六、HDFS文件和文件夹判断
@Test
public void testListStatus() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
// 1 获取hdfs客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://SZMaster01:9000"), conf, "hadoop");
// 2 进行判断
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}
// 3 关闭资源
fs.close();
System.out.println("over");
}