HDFS手动拷贝某个特定的数据块(比如某个文件的第二个数据块)

代码呈现

package api;


import java.io.File;
import java.io.FileOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

/**
 * 手动拷贝某个特定的数据块(比如某个文件的第二个数据块)
 * @author potter
 *
 */
public class TestCat {

    public static void main(String[] args) throws Exception {
        
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://potter2:9000");
        System.setProperty("HADOOP_USER_NAME", "potter");
        FileSystem fs = FileSystem.get(conf);
        
        FSDataInputStream in = fs.open(new Path("/111.zip"));
        System.out.println(in+",1111111");
        //获取文件信息
        FileStatus[] listStatus = fs.listStatus(new Path("/111.zip"));
        System.out.println(listStatus.length+",2222222");
        //获取文件块的信息
        BlockLocation[] blockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());
        System.out.println(blockLocations.length+",3333333");
        
        //获取第二个块的长度
        Long lenght = blockLocations[1].getLength();
        System.out.println(lenght+",444444444");
        //获取第二个块的起始偏移量
        Long offset = blockLocations[1].getOffset();
        System.out.println(offset+",5555555");
        in.seek(offset);
        FileOutputStream os = new FileOutputStream(new File("D:/block2"));
        
        //获取第二个块的写入输出流
        IOUtils.copyBytes(in, os, lenght, true);
        
        in.close();
        os.close();
    }
}

 

转载于:https://www.cnblogs.com/sangumaolu/p/8545900.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值