HDFS 简介
hdfs是hadoop中分布式的文件存储系统;具有高容错、可扩展;广泛的用于大数据项目中(不仅仅是hadoop)
将257M的文件上传到hdfs
[hadoop@hadoop000 spark-2.4.3]$ hdfs dfs -put spark-2.4.3-bin-2.6.0-cdh5.15.1.tgz /test/
第一种:按文件块下载文件,然后拼接为一个完整的文件
1.下载文件的第一个数据块:
@Test
public void downLoad2() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/test/spark-2.4.3-bin-2.6.0-cdh5.15.1.tgz"));
FileOutputStream out = new FileOutputStream(new File("E:/test.tgz.part0"));
byte[] buffer = new byte[1024];
for(int i=0;i<1024 * 128;i++){
in.read(buffer);
out.write(buffer);
}
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
2.下载文件的第二个数据块:
@Test
public void downLoad2() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/test/spark-2.4.3-bin-2.6.0-cdh5.15.1.tgz"));
FileOutputStream out = new FileOutputStream(new File("E:/test.tgz.part1"));
byte[] buffer = new byte[1024];
//跳过第一块数据的偏移量
in.seek(1024*1024*128);
for(int i=0;i<1024 * 128;i++){
in.read(buffer);
out.write(buffer);
}
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
3.下载文件的第三个数据块:
@Test
public void downLoad2() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/test/spark-2.4.3-bin-2.6.0-cdh5.15.1.tgz"));
FileOutputStream out = new FileOutputStream(new File("E:/test.tgz.part2"));
//跳过第一、二块数据的偏移量
in.seek(1024*1024*128*2);
IOUtils.copyBytes(in,out,configuration);
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
4.在widows 黑窗口中将三个数据块合并为一个完整的文件
type test.tgz.part1 >> test.tgz.part0
type test.tgz.part2 >> test.tgz.part0
5.重名,查看文件,文件没有损坏。
6.注意细节 【在蓝色窗口执行,合并文件,文件会报损坏】
第二种:一次下载完整的文件
@Test
public void downLoad1() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/test/spark-2.4.3-bin-2.6.0-cdh5.15.1.tgz"));
FileOutputStream out = new FileOutputStream(new File("E:/test.tgz"));
IOUtils.copyBytes(in,out,4096);
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}