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
提示:wmic命令 调整dos窗口大小,quit 退出