HDFS系列(6) | HDFS的java API操作
【摘要】
在之前的博客《HDFS系列(5) |进行API操作前的准备》 中,博主为大家分享的是在进行API操作前的准备工作。而本篇博客,博主为大家展现HDFS的API操作。
目录
1. HDFS文件上传2. HDFS文件下载3. HDFS文件夹删除4. HDFS文件名更改5. HDFS文件详情查看6. HDFS文件和文件夹判断
1. HDFS文件上传...
在之前的博客《HDFS系列(5) |进行API操作前的准备》
中,博主为大家分享的是在进行API操作前的准备工作。而本篇博客,博主为大家展现HDFS的API操作。
1. HDFS文件上传
- 1. 源码:
package com.buwenbuhuo.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author buwenbuhuo
* @create 2020-04-22 16:45
* com.buwenbuhuo.hdfs - the name of the target package where the new class or interface will be created.
* hdfs0422 - the name of the current project.
*/
public class HDFSClient { @Test public void testMkdirs() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统 Configuration configuration = new Configuration(); // 配置在集群上运行 FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 2 上传文件 fs.copyFromLocalFile(new Path("d:/buwenbuhuo.txt"), new Path("/buwenbuhuo.txt")); // 3 关闭资源 fs.close(); System.out.println("over"); } }
- 2. 运行结果
2. HDFS文件下载
- 1. 源码
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/buwenbuhuo.txt"), new Path("d:/buwenbuhuo1.txt"), true); // 3 关闭资源
fs.close();
}
- 2. 结果:
3. HDFS文件夹删除
- 1. 源码:
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 2 执行删除
fs.delete(new Path("/0422/"), true); // 3 关闭资源
fs.close();
}
- 2.结果
4. HDFS文件名更改
- 1.源码:
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 2 修改文件名称
fs.rename(new Path("/buwenbuhuo.txt"), new Path("/VN1.txt")); // 3 关闭资源
fs.close();
}
- 2. 结果
5. HDFS文件详情查看
- 1. 源码:
# 查看文件名称、权限、长度、块信息
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 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();
}
- 2. 结果
6. HDFS文件和文件夹判断
- 1. 源码:
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata"); // 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();
}
- 2. 结果
为了方便大家理解,在代码中博主都写有注释,因此在这里就不作过多的过程说明了。那么本次的分享就到这里了,小伙伴们有什么疑惑或好的建议可以积极在评论区留言,博主后续还会推出HDFS系列的其他内容,希望大家持续关注博主!
文章来源: buwenbuhuo.blog.csdn.net,作者:不温卜火,版权归原作者所有,如需转载,请联系作者。
原文链接:buwenbuhuo.blog.csdn.net/article/details/105687428
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)