黑猴子的家:定位读取HDFS文件

1、Code -> GitHub

https://github.com/liufengji/hadoop_hdfs.git

2、下载第一块

@Test
// 定位下载第一块内容
public void readFileSeek1() throws Exception {

    // 1 创建配置信息对象
    Configuration configuration = new Configuration();
    FileSystem fs = FileSystem.get(new URI("hdfs://node1:9000"), configuration, "victor");

    // 2 获取输入流路径
    Path path = new Path("hdfs://node1:9000/user/victor/tmp/hadoop-2.8.2.tar.gz");

    // 3 打开输入流
    FSDataInputStream fis = fs.open(path);

    // 4 创建输出流
    FileOutputStream fos = new FileOutputStream("e:/hadoop-2.8.2.tar.gz.part1");

    // 5 流对接
    byte[] buf = new byte[1024];
    for (int i = 0; i < 128 * 1024; i++) {
        fis.read(buf);
        fos.write(buf);
    }

    // 6 关闭流
    IOUtils.closeStream(fis);
    IOUtils.closeStream(fos);
}

3、下载第二块

    @Test
    // 定位下载第二块内容
    public void readFileSeek2() throws Exception{
        
        // 1 创建配置信息对象
        Configuration configuration = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9000"), configuration, "victor");
        
        // 2 获取输入流路径
        Path path = new Path("hdfs://node1:9000/user/victor/tmp/hadoop-2.8.2.tar.gz");
        
        // 3 打开输入流
        FSDataInputStream fis = fs.open(path);
        
        // 4 创建输出流
        FileOutputStream fos = new FileOutputStream("e:/hadoop-2.8.2.tar.gz.part2");
        
        // 5 定位偏移量(第二块的首位)
        fis.seek(1024 * 1024 * 128);
        
        // 6 流对接
        IOUtils.copyBytes(fis, fos, 1024);
        
        // 7 关闭流
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }

4、合并文件

在window命令窗口中执行(window+R)→ cmd

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

9193428-e3c36bce0c2c71ea.png

 

提示:wmic命令 调整dos窗口大小,quit 退出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值