把/home/hadoop/MyTmp/test.txt文件上传到HDFS根目录
// 把/home/hadoop/MyTmp/test.txt文件上传到HDFS根目录
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
// 1 获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");
// 2 获取输入流
// FileInputStream fis = new FileInputStream(new File("/home/hadoop/MyTmp/test.txt"));
FileInputStream fis = new FileInputStream(new File("/home/hadoop/Downloads/hadoop-2.7.1.tar.gz"));
// 3 获取输出流
// FSDataOutputStream fos = fs.create(new Path("/putFileToHDFS1.txt"));
FSDataOutputStream fos = fs.create(new Path("/hadoop-2.7.1.tar.gz"));
// 4 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
// 从HDFS上下载putFileToHDFS1文件到/home/hadoop/MyTmp/getFileFromHDFS1.txt上
// 从HDFS上下载putFileToHDFS1文件到/home/hadoop/MyTmp/getFileFromHDFS1.txt上
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{
// 1 获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hoop");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/putFileToHDFS1.txt"));
// 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/getFileFromHDFS1.txt"));
// 4 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
分块下载文件
// 下载第一块
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
// 1 获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.1.tar.gz"));
// 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/hadoop-2.7.1.tar.gz.part1"));
// 4 流的对拷(只拷贝128m)
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read(buf);
fos.write(buf);
}
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
// 下载第二块
@SuppressWarnings("resource")
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf , "hadoop");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.1.tar.gz"));
// 3 设置指定读取的起点
fis.seek(1024*1024*128);
// 4 获取输出流
FileOutputStream fos = new FileOutputStream(new File("/home/hadoop/MyTmp/hadoop-2.7.1.tar.gz.part2"));
// 5 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 6 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
后面可将两个文件块进行合并,结果是保持一致的