首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

压缩 HDFS 下的文件, 提供使用者上載

2012-09-23 
压缩 HDFS 上的文件, 提供使用者下載(转)今天的進度是研究如何讓使用者透過網路來下載 HDFS?上的檔案,基本

压缩 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(); } }

热点排行