Hadoop之HDFS的应用(Java API实现文件上传与下载)

HDFS客户端环境搭建就不在这里赘述了,不知道怎么搭建读者可参考这篇博客:https://blog.csdn.net/weixin_43800761/article/details/106854426

一、HDFS文件上传

  1. 源代码编写
@Test
    public void testCopyFromLocalFile() {
        // 创建作业,配置作业所需参数
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
        FileSystem fs = null;
        try {
            fs = FileSystem.get(conf);
            //定义文件路径和上传路径
            Path src = new Path("E://hello.txt");
            Path dest = new Path("/test/hello.txt");
            //从本地上传到服务器上
            fs.copyFromLocalFile(src,dest);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("上传成功!");
        logger.info("uploadFile success");
    }
  1. 测试结果
    在这里插入图片描述

二、文件下载

代码编写

    @Test
    public void testCopyToLocalFile() throws IOException {

        // 1 获取文件系统
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
        FileSystem fs = FileSystem.get(conf);

        // 2 执行下载操作
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(false, new Path("/test/hello.txt"), new Path("e:/hello1.txt"), true);

        // 3 关闭资源
        fs.close();
    }

三、不使用框架方式

上面我们学的API操作HDFS系统都是框架封装好的。那么如果我们想自己实现上述API的操作该怎么实现呢?
我们可以采用IO流的方式实现数据的上传和下载。

3.1 文件上传

把本地e盘上的hello.txt文件上传到HDFS根目录

@Test
public void putFileToHDFS() throws IOException{

	// 1 获取文件系统
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
    FileSystem fs = FileSystem.get(conf);

	// 2 创建输入流
	FileInputStream fis = new FileInputStream(new File("e:/hello.txt"));

	// 3 获取输出流
	FSDataOutputStream fos = fs.create(new Path("/hello.txt"));

	// 4 流对拷
	IOUtils.copyBytes(fis, fos, configuration);

	// 5 关闭资源
	IOUtils.closeStream(fos);
	IOUtils.closeStream(fis);
    fs.close();
}

3.2 文件下载

从HDFS上下载hello.txt文件到本地e盘上

@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{

	// 1 获取文件系统
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
    FileSystem fs = FileSystem.get(conf);
		
	// 2 获取输入流
	FSDataInputStream fis = fs.open(new Path("/hello.txt"));
		
	// 3 获取输出流
	FileOutputStream fos = new FileOutputStream(new File("e:/hello.txt"));
		
	// 4 流的对拷
	IOUtils.copyBytes(fis, fos, configuration);
		
	// 5 关闭资源
	IOUtils.closeStream(fos);
	IOUtils.closeStream(fis);
	fs.close();
}

3.2 定位文件读取

分块读取HDFS上的大文件,比如根目录下的/hadoop-3.1.2.tar.gz

(1)下载第一块

@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{

	// 1 获取文件系统
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
    FileSystem fs = FileSystem.get(conf);
		
	// 2 获取输入流
	FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.2.tar.gz"));
		
	// 3 创建输出流
	FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-3.1.2.tar.gz.part1"));
		
	// 4 流的拷贝
	byte[] buf = new byte[1024];
		
	for(int i =0 ; i < 1024 * 128; i++){
		fis.read(buf);
		fos.write(buf);
	}
		
	// 5关闭资源
	IOUtils.closeStream(fis);
	IOUtils.closeStream(fos);
fs.close();
}

(2)下载第二块

@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{

	// 1 获取文件系统
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS","hdfs://192.168.137.150:9000");
    FileSystem fs = FileSystem.get(conf);
		
	// 2 打开输入流
	FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.2.tar.gz"));
		
	// 3 定位输入数据位置
	fis.seek(1024*1024*128);
		
	// 4 创建输出流
	FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-3.1.2.tar.gz.part2"));
		
	// 5 流的对拷
	IOUtils.copyBytes(fis, fos, configuration);
		
	// 6 关闭资源
	IOUtils.closeStream(fis);
	IOUtils.closeStream(fos);
}

(3)合并文件
在Window命令窗口中进入到目录E:\,然后执行如下命令,对数据进行合并

type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1

合并完成后,将hadoop-2.7.2.tar.gz.part1重新命名为hadoop-2.7.2.tar.gz。解压发现该tar包非常完整。

猜你喜欢

转载自blog.csdn.net/weixin_43800761/article/details/106856690