千家信息网

java如何使用IO流的方式实现hdfs数据的上传和下载

发表于:2024-10-05 作者:千家信息网编辑
千家信息网最后更新 2024年10月05日,这篇文章给大家分享的是有关java如何使用IO流的方式实现hdfs数据的上传和下载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。IO操作-文件上传1、把本地d盘下的hell
千家信息网最后更新 2024年10月05日java如何使用IO流的方式实现hdfs数据的上传和下载

这篇文章给大家分享的是有关java如何使用IO流的方式实现hdfs数据的上传和下载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

IO操作-文件上传

1、把本地d盘下的hello.txt文件上传到HDFS根目录

2、代码编写

@Test    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {        // 1 获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");        // 2 创建输入流        FileInputStream fis = new FileInputStream(new File("d:/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、浏览器输入http://hadoop100:50070/explorer.html#/查看是否上传

IO操作-文件下载

1、从HDFS下载hello.txt文件到d盘根目录下

2、代码编写

    @Test    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException {        // 1 获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");        // 2 获取输入流        FSDataInputStream fis = fs.open(new Path("/hello.txt"));        // 3 获取输出流        FileOutputStream fos = new FileOutputStream(new File("d:/hello.txt"));        // 4 流的对拷        IOUtils.copyBytes(fis, fos, configuration);        // 5 关闭资源        IOUtils.closeStream(fos);        IOUtils.closeStream(fis);        fs.close();    }

3、查看目录下是否有文件

IO操作-分块读取

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

2、下载第一块,代码如下

@Testpublic void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root"); // 2 获取输入流 FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz")); // 3 创建输出流 FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.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();}

3、下载第二块,代码如下

@Testpublic void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root"); // 2 打开输入流 FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz")); // 3 定位输入数据位置 fis.seek(1024*1024*128); // 4 创建输出流 FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part2")); // 5 流的对拷 IOUtils.copyBytes(fis, fos, configuration); // 6 关闭资源 IOUtils.closeStream(fis); IOUtils.closeStream(fos);}

4、合并文件,把两块文件合并为一个

在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包非常完整。

感谢各位的阅读!关于"java如何使用IO流的方式实现hdfs数据的上传和下载"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0