HDFS系列(6) | HDFS的java API操作

在之前的博客《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. 运行结果
    1
    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
    4

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.结果
    5

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. 结果

6

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. 结果
    7

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. 结果
    8

为了方便大家理解,在代码中博主都写有注释,因此在这里就不作过多的过程说明了。那么本次的分享就到这里了,小伙伴们有什么疑惑或好的建议可以积极在评论区留言,博主后续还会推出HDFS系列的其他内容,希望大家持续关注博主!

发布了75 篇原创文章 · 获赞 74 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_16146103/article/details/105687428