HDFS IO流操作
一、IO流文件上传
@Test public void testIOPut() throws URISyntaxException, IOException, InterruptedException { // 1. 获取对象 Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom"); // 2. 获取输入流 FileInputStream fIS = new FileInputStream(new File("D:\\test.txt")); // 3. 获取输出流 FSDataOutputStream fOS = fs.create(new Path("/lili.txt")); // 4. 流的对拷 IOUtils.copyBytes(fIS, fOS, conf); // 5. 关闭资源 IOUtils.closeStream(fOS); IOUtils.closeStream(fIS); fs.close(); }
二、IO流文件下载
@Test public void testIODownload() throws URISyntaxException, IOException, InterruptedException { // 1. 获取对象 Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom"); // 2. 获取输入流 FSDataInputStream fIS = fs.open(new Path("/abc.txt")); // 3. 获取输出流 FileOutputStream fOI = new FileOutputStream(new File("E:\\abc\\abc.txt")); // 4. 流的对拷 IOUtils.copyBytes(fIS, fOI, conf); // 5. 关闭资源 IOUtils.closeStream(fOI); IOUtils.closeStream(fIS); fs.close(); }
三、定位读取文件
// 下载第一块 @Test public void testLocation() throws URISyntaxException, IOException, InterruptedException { // 1.获取对象 Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom"); // 2.获取输入流 FSDataInputStream fIS = fs.open(new Path("/jdk-8u261-linux-x64.tar.gz")); // 3.获取输出流 FileOutputStream fOS = new FileOutputStream(new File("E:\\abc\\jdk-8u261-linux-x64.tar.gz")); // 4.流的对拷(只copy128) 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(); } // 下载第二块 @Test public void testDownloadTwo() throws URISyntaxException, IOException, InterruptedException { // 1. 获取对象 Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom"); // 2. 获取输入流 FSDataInputStream fIS = fs.open(new Path("/jdk-8u261-linux-x64.tar.gz")); // 3. 设置指定读取的起点 fIS.seek(1024*1024*128); // 4. 获取输出流 FileOutputStream fOS = new FileOutputStream(new File("E:\\abc\\jdk.tar.gz.part2")); // 5. 流的对拷 IOUtils.copyBytes(fIS, fOS, conf); // 6. 关闭连接 IOUtils.closeStream(fOS); IOUtils.closeStream(fIS); fs.close(); }