Hadoop学习——hdfs的数据流操作

把/home/hadoop/MyTmp/test.txt文件上传到HDFS根目录

 // 把/home/hadoop/MyTmp/test.txt文件上传到HDFS根目录
    @Test
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");

        // 2 获取输入流
//        FileInputStream fis = new FileInputStream(new File("/home/hadoop/MyTmp/test.txt"));
        FileInputStream fis = new FileInputStream(new File("/home/hadoop/Downloads/hadoop-2.7.1.tar.gz"));

        // 3 获取输出流
//        FSDataOutputStream fos = fs.create(new Path("/putFileToHDFS1.txt"));
        FSDataOutputStream fos = fs.create(new Path("/hadoop-2.7.1.tar.gz"));

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

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

// 从HDFS上下载putFileToHDFS1文件到/home/hadoop/MyTmp/getFileFromHDFS1.txt上

   // 从HDFS上下载putFileToHDFS1文件到/home/hadoop/MyTmp/getFileFromHDFS1.txt上
    @Test
    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hoop");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/putFileToHDFS1.txt"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/getFileFromHDFS1.txt"));

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

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

分块下载文件

 // 下载第一块
    @Test
    public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.1.tar.gz"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/hadoop-2.7.1.tar.gz.part1"));

        // 4 流的对拷(只拷贝128m)
        byte[] buf = new byte[1024];
        for (int i = 0; i < 1024 * 128; i++) {
            fis.read(buf);
            fos.write(buf);
        }

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

    // 下载第二块
    @SuppressWarnings("resource")
    @Test
    public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.1.tar.gz"));

        // 3 设置指定读取的起点
        fis.seek(1024*1024*128);

        // 4 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/hadoop-2.7.1.tar.gz.part2"));

        // 5 流的对拷
        IOUtils.copyBytes(fis, fos, conf);

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

后面可将两个文件块进行合并,结果是保持一致的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值