压缩 HDFS 上的文件, 提供使用者下
(转)今天的度是研究如何使用者透路下 HDFS?上的案,基本上和一般的案什,直接透 java 建的 java.util.zip 套件就可以易做到了。唯一的差,在要用 Hadoop API 提供的 FSDatainputStream 案串流,然後逐一入到串流就可以完成案的目的。?
而在操作流程的上,使用者先要下的案, HTML 有基解的人不是什,利用 Form 加 Checkbox 就可以易的成目的,使用者後,再送到 servlet 理就可以了。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String photoYear = request.getParameter("photoYear"); String id = request.getParameter("id"); Configuration conf = new Configuration(); conf.set("hadoop.job.ugi","hadoop,supergroup"); String uriBase= String.format("hdfs://cloud:9000/%s/%s/", photoYear, id); String files[] = request.getParameterValues("SelectPic"); BufferedOutputStream dest = new BufferedOutputStream(response.getOutputStream()); ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(dest)); response.setHeader("Content-Type", "application/zip");int bytesRead;Path sourceFilePath;FileSystem fs = FileSystem.get(URI.create(uriBase),conf);try { for (int i=0; i < files.length; i++) { sourceFilePath = new Path(uriBase + files[i]); //料入串流 FSDataInputStream in = fs.open(sourceFilePath); //建立案的 entry ZipEntry entry = new ZipEntry(files[i]); outZip.putNextEntry(entry); //串流移到此 entry 的料位置 //透案入串流, HDFS 案容入到串流 byte[] buffer = new byte[4096]; while ((bytesRead = in.read(buffer)) < 0) { outZip.write(buffer, 0, bytesRead); } in.close(); } outZip.flush(); outZip.close(); } catch(Exception e) { outZip.close(); e.printStackTrace(); } }