2021-03-15 03:05:18
Hadoop入门教程(六):Hadoop API 使用编程的方式操作 HDFS
教程索引目录请访问:《大数据技术入门级系列教程》
上一篇讲了使用 Shell 命令操作 HDFS,但实际中我们肯定不可能一直手动操作,还是需要通过编程实现自动化的,所以本文将带你熟悉一下使用 Java 编程控制 HDFS 中的文件。
先决条件
本章内容默认读者已经具备 Java 编程能力,其中包括 JavaSE 基础知识、Maven 构建知识,如果您还不会使用 Java/Maven 构建项目,请先学习相关知识。并且已经阅读过前面的章节搭建起了 Hadoop 平台,如果您还没有搭建起 Hadoop 平台,可能无法操作本章的内容。本章的完整代码分享在:https://github.com/renfei/demo/tree/master/hadoop/hadoop_api
Maven项目创建
我们需要先创建一个 Maven 项目,并引用相关的依赖,Maven 是什么在这里不讨论了,主要讨论 Hadoop 的 HDFS,请自行学习 Maven,这里我贴一下 Maven 的 pom.xml 中的依赖:
<properties>
<hadoop.version>2.10.1</hadoop.version>
<log4j.version>2.14.0</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
HDFS的API操作
HDFS创建文件夹
public void mkdirs() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.mkdirs(new Path("/demo"));
fileSystem.close();
}
HDFS文件上传
public void upload() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.copyFromLocalFile(new Path("/Users/renfei/Downloads/demo.txt"), new Path("/demo/demo.txt"));
fileSystem.close();
}
HDFS文件下载
public void get() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.copyToLocalFile(new Path("/demo/demo.txt"), new Path("/Users/renfei/Downloads/demo2.txt"));
fileSystem.close();
}
HDFS文件重命名
public void rename() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.rename(new Path("/demo/demo.txt"), new Path("/demo/demo2.txt"));
fileSystem.close();
}
HDFS文件详情获取
public void listFiles() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/demo/"), 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("---------------------");
}
fileSystem.close();
}
HDFS文件删除
public void delete() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
// 此处第二参数 true 是指递归删除
if (fileSystem.delete(new Path("/demo"), true)) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
fileSystem.close();
}
HDFS文件流式上传
public void upload() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
// 创建一个文件输入流
FileInputStream fileInputStream = new FileInputStream(new File("/Users/renfei/Downloads/demo.txt"));
// 获取输出流
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/demo/demo.txt"));
// 流拷贝
IOUtils.copyBytes(fileInputStream, fsDataOutputStream, configuration);
// 关闭资源
IOUtils.closeStream(fsDataOutputStream);
IOUtils.closeStream(fileInputStream);
fileSystem.close();
}
HDFS文件流式下载
public void get() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
// 获取输入流
FSDataInputStream fsDataOutputStream = fileSystem.open(new Path("/demo/demo.txt"));
// 获取输出流
FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/renfei/Downloads/demo2.txt"));
// 流拷贝
IOUtils.copyBytes(fsDataOutputStream, fileOutputStream, configuration);
// 关闭资源
IOUtils.closeStream(fsDataOutputStream);
IOUtils.closeStream(fileOutputStream);
fileSystem.close();
}
商业用途请联系作者获得授权。
版权声明:本文为博主「任霏」原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号内容标签
最新留言
- 你写得非常清晰明了,让我很容易理解你的观点。
- 感谢分享!拿走了~
- 您是说 UCClient 类接收来自Discuz的UCenter的消息吧,请求是来自 Discuz 的 UCenter吗?code 为 null 说明请求URL地址中没有 code 参数 (?code=xxx) ,确定是 UCenter 发起的请求吗?
- String code = request.getParameter("code"); code一直是null 这是为什么啊
- 你好,我想问一下如果是分析型的数据库要怎么制作docker镜像呢 是修改V008R003C002B0320版本号吗
- 可以的,我也正在开发分享的程序,可以邮件或群联系我都可以,关于页面里有联系方式:https://www.renfei.net/page/about 。
- 有破解软件的需要可以私下联系您吗?
- 您好,手机APP只是个客户端,用于数据呈现展示,数据均保存在服务器上,只留个APP没有任何用处,无能为力哦。
- 老哥 看你弄了这么多软件好厉害啊。 我有个软件 我买过几个小会员 没用几天 然后商家跑路了,软件服务器关闭了,连不上去 用不了。 你能做成一个打补丁版本可以本地用的么? 方便看下么?https://haodezhe.lanzouw.com/iD0f30h9joza 谢谢老哥!
- 您好,由于版权投诉和我国知识产权法的完善,我已经下架所有破解软件的下载链接了。
热文排行
- 生花妙笔信手来 – 基于 Amazon SageMaker 使用 Grounded-SAM 加速电商广告素材生成 [1]
- github.renfei.net 不再完整代理 Github 页面改为代理指定文件
- 优雅的源代码管理(三):本地优雅的使用 Git Rebase 变基
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)
- ChatGPT 开发商 OpenAI 买下极品域名 AI.com
- 火爆的 AI 人工智能 ChatGPT 国内注册教程、使用方式和收费标准
- 解决 SpringCloud 中 bootstrap.yml 不识别 @activatedProperties@ 参数
- Cron表达式书写教程搞定Linux、Spring、Quartz的定时任务
- 阿里云香港可用区C发生史诗级故障