上一篇讲了使用 Shell 命令操作 HDFS,但实际中我们肯定不可能一直手动操作,还是需要通过编程实现自动化的,所以本文将带你熟悉一下使用 Java 编程控制 HDFS 中的文件。
本章内容默认读者已经具备 Java 编程能力,其中包括 JavaSE 基础知识、Maven 构建知识,如果您还不会使用 Java/Maven 构建项目,请先学习相关知识。并且已经阅读过前面的章节搭建起了 Hadoop 平台,如果您还没有搭建起 Hadoop 平台,可能无法操作本章的内容。本章的完整代码分享在:https://github.com/renfei/demo/tree/master/hadoop/hadoop_api
我们需要先创建一个 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>
public void mkdirs() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.mkdirs(new Path("/demo"));
fileSystem.close();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://www.renfei.net/posts/1003464
本站有缓存策略,时间约2小时后能看到您的评论。本站使用自动审核机制,如果您的内容包含广告/谩骂/恐怖/暴力/涉政等不和谐内容将无法展示!
本站有缓存策略,时间约2小时后能看到您的评论。本站使用自动审核机制,如果您的内容包含广告/谩骂/恐怖/暴力/涉政等不和谐内容将无法展示!