Hadoop-HDFS用户自定义上传和下载

    科技2022-08-06  99

    用户自定义上传文件大小

    需求:我把这个文件的前20kb上传。

    // 只上传文件的前20K /* * 官方的实现 * InputStream in=null; OutputStream out = null; try { in = srcFS.open(src); out = dstFS.create(dst, overwrite); IOUtils.copyBytes(in, out, conf, true); } catch (IOException e) { IOUtils.closeStream(out); IOUtils.closeStream(in); throw e; } */ @Test public void testCustomUpload() throws Exception { Configuration conf=new Configuration(); FileSystem windows=FileSystem.get(conf); FileSystem linux=FileSystem.get(new URI("hdfs://hadoop1:9000"), conf, "ygp"); //提供两个Path,和两个FileSystem Path src=new Path("C:/Users/Lenovo/Desktop/乔老师/05534745.pdf"); Path dest=new Path("/05534745(20k).pdf"); // 使用本地文件系统中获取的输入流读取本地文件 FSDataInputStream is = windows.open(src); // 使用HDFS的分布式文件系统中获取的输出流,向dest路径写入数据 FSDataOutputStream os = linux.create(dest, true); // 1k byte [] buffer=new byte[1024]; // 流中数据的拷贝 for (int i = 0; i <20; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream(is); IOUtils.closeStream(os); }

    但是我这报了一个错,说我本地的那个路径不是一个有效的分布式文件名?? 然后resource目录的自定义的core-site.xml删除之后就成功了。

    用户自定义下载

    我现在HDFS上有一个670MB的文件PPT.zip, 显然可分为6个块

    需求:下载某一个块 先下载第一块

    @Test public void testFirstBlock() throws Exception { Configuration conf=new Configuration(); FileSystem windows=FileSystem.get(conf); FileSystem linux=FileSystem.get(new URI("hdfs://hadoop1:9000"), conf, "ygp"); //提供两个Path,和两个FileSystem Path src=new Path("/PPT.zip"); Path dest=new Path("D:/firstblock"); // 使用HDFS的分布式文件系统中获取的输入流,读取HDFS上指定路径的数据 FSDataInputStream is = linux.open(src); // 使用本地文件系统中获取的输出流写入本地文件 FSDataOutputStream os = windows.create(dest, true); // 1k byte [] buffer=new byte[1024]; // 流中数据的拷贝 for (int i = 0; i < 1024 * 128; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream(is); IOUtils.closeStream(os); }

    再下载第二个块

    @Test public void testSecondBlock() throws Exception { Configuration conf=new Configuration(); FileSystem windows=FileSystem.get(conf); FileSystem linux=FileSystem.get(new URI("hdfs://hadoop1:9000"), conf, "ygp"); //提供两个Path,和两个FileSystem Path src=new Path("/PPT.zip"); Path dest=new Path("D:/secondblock"); // 使用HDFS的分布式文件系统中获取的输入流,读取HDFS上指定路径的数据 FSDataInputStream is = linux.open(src); // 使用本地文件系统中获取的输出流写入本地文件 FSDataOutputStream os = windows.create(dest, true); //定位到流的指定位置 is.seek(1024*1024*128); // 1k byte [] buffer=new byte[1024]; // 流中数据的拷贝 for (int i = 0; i < 1024 * 128; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream(is); IOUtils.closeStream(os); }

    第三块,第四块以此类推,我们来看看最后一块怎么下载

    @Test public void testFinalBlock() throws Exception { Configuration conf=new Configuration(); FileSystem windows=FileSystem.get(conf); FileSystem linux=FileSystem.get(new URI("hdfs://hadoop1:9000"), conf, "ygp"); //提供两个Path,和两个FileSystem Path src=new Path("/PPT.zip"); Path dest=new Path("D:/lastblock"); // 使用HDFS的分布式文件系统中获取的输入流,读取HDFS上指定路径的数据 FSDataInputStream is = linux.open(src); // 使用本地文件系统中获取的输出流写入本地文件 FSDataOutputStream os = windows.create(dest, true); //定位到流的指定位置 is.seek(1024*1024*128*5); //buffSize 默认不能超过4096 IOUtils.copyBytes(is, os, 4096, true); }

    当所有块下载完成后,我们在cmd窗口中用type命令,依次将第2,3,4,5,6块的内容追加到第一块里面,然后将第一块文件的名字重命名为PPT.zip,然后我们将其解压打开,判断是否里面每个ppt都能打开。

    Processed: 0.010, SQL: 8