手动拷贝HDFS上大文件的某个特定的数据块,我拷贝的是hadoop安装包的第二块
package com.zzm.day03;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import java.io.*;
public class AssignBlock {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(new Path("/hadoop-2.7.6.tar.gz"));
FileStatus[] listStatus = fs.listStatus(new Path("hadoop-2.7.6.tar.gz"));
BlockLocation[] blockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());
long length = blockLocations[1].getLength();
long offset = blockLocations[1].getOffset();
in.seek(offset);
FileOutputStream out = new FileOutputStream(new File("/Users/zhaoziming/IO/zzm.tar.gz"));
IOUtils.copyBytes(in, out, length, true);
}
}