91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

貼一段java讀取hdfs 解壓gz zip tar.gz保存到hdfs的代碼

發布時間:2020-07-02 17:05:53 來源:網絡 閱讀:4595 作者:ranfengzheng 欄目:大數據

package main.java;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.*;


import org.apache.commons.compress.archivers.ArchiveException;

import org.apache.commons.compress.archivers.ArchiveInputStream;

import org.apache.commons.compress.archivers.ArchiveStreamFactory;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

import java.io.IOException;
import java.net.URI;

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;;


/**

* 解壓tar.gz  zip  gz文件包 這里的數據源和輸出目錄都為HDFS

*

*/

public class GZipHdfs {


   private BufferedOutputStream bufferedOutputStream;
   String zipfileName = null;


   public GZipHdfs(String fileName) {

       this.zipfileName = fileName;

   }

   /*

    * 執行入口,rarFileName為需要解壓的文件路徑(具體到文件),destDir為解壓目標路徑  路徑為HDFS

    */

   public List<String> unTargzFile(String rarFileName, String destDir) throws IOException {

       GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();
       FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

       List<String> r = GZipHdfs.defUnTargzFile(outputDirectory, fs);
       fs.close();
       return r;

   }


   public List<String> defUnTargzFile(String outputDirectory, FileSystem fs) {

       FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> tarList = new LinkedList<String>();

       try {

           FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));


           GZIPInputStream is = new GZIPInputStream(new BufferedInputStream(
                   hdfsInputStream));

           in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);

           bufferedInputStream = new BufferedInputStream(in);

           TarArchiveEntry entry = (TarArchiveEntry) in.getNextEntry();


           while (entry != null) {

               String name = entry.getName();

               String[] names = name.split("/");

               String fileName = outputDirectory;

               for (int i = 0; i < names.length; i++) {

                   String str = names[i];

                   fileName = fileName + "/" + str;

               }

               FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));


               bufferedOutputStream = new BufferedOutputStream(
                       hdfsOutStream);

               int b;

               while ((b = bufferedInputStream.read()) != -1) {

                   bufferedOutputStream.write(b);

               }

               bufferedOutputStream.flush();

               bufferedOutputStream.close();


               entry = (TarArchiveEntry) in.getNextEntry();

               tarList.add(name);

           }


       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       } catch (ArchiveException e) {

           e.printStackTrace();

       } finally {

           try {

               if (bufferedInputStream != null) {

                   bufferedInputStream.close();


               }

           } catch (IOException e) {

               e.printStackTrace();

           }

       }
       return tarList;

   }
   /*

    * 執行入口,rarFileName為需要解壓的文件路徑(具體到文件),destDir為解壓目標路徑  路徑為HDFS

    */

   public List<String> unZipFile(String rarFileName, String destDir) throws IOException {

       GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();

       FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

       List<String> r = GZipHdfs.defUnZipFile(outputDirectory, fs);
       fs.close();
       return r;

   }


   public List<String> defUnZipFile(String outputDirectory, FileSystem fs) {

       FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> zipList = new LinkedList<String>();

       try {


           FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));
           ZipInputStream is = new ZipInputStream(new BufferedInputStream(
                   hdfsInputStream));


           bufferedInputStream = new BufferedInputStream(is);
           ZipEntry entry =is.getNextEntry();


           while (entry != null) {

               String name = entry.getName();

               String[] names = name.split("/");

               String fileName = outputDirectory;

               for (int i = 0; i < names.length; i++) {

                   String str = names[i];

                   fileName = fileName + "/" + str;

               }

               FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));

               bufferedOutputStream = new BufferedOutputStream(
                       hdfsOutStream);

               int b;

               while ((b = bufferedInputStream.read()) != -1) {

                   bufferedOutputStream.write(b);

               }

               bufferedOutputStream.flush();

               bufferedOutputStream.close();

               entry = (ZipEntry) is.getNextEntry();

               zipList.add(name);

           }


       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (bufferedInputStream != null) {

                   bufferedInputStream.close();


               }

           } catch (IOException e) {

               e.printStackTrace();

           }

       }
       return zipList;

   }
   /*

    * 執行入口,rarFileName為需要解壓的文件路徑(具體到文件),destDir為解壓目標路徑  路徑為HDFS

    */


   public List<String> unGZipFile(String rarFileName, String destDir) throws IOException {

       GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();
       FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

       List<String> r = GZipHdfs.defUnGZipFile(outputDirectory, fs);
       fs.close();
       return r;

   }


   public List<String> defUnGZipFile(String outputDirectory, FileSystem fs) {

       FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> tarList = new LinkedList<String>();

       try {

           FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));


           GzipCompressorInputStream is = new GzipCompressorInputStream(new BufferedInputStream(
                   hdfsInputStream));
           bufferedInputStream = new BufferedInputStream(is);


           String[] nameList = zipfileName.split("/");
           String name=nameList[nameList.length-1].replace(".gz","");
           String fileName = outputDirectory+"/"+name;
           FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));
           bufferedOutputStream = new BufferedOutputStream(
                   hdfsOutStream);
           int b;
           while ((b = bufferedInputStream.read()) != -1) {
               bufferedOutputStream.write(b);

           }
           bufferedOutputStream.flush();
           bufferedOutputStream.close();
           tarList.add(name);




       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (bufferedInputStream != null) {

                   bufferedInputStream.close();


               }

           } catch (IOException e) {

               e.printStackTrace();

           }

       }
       return tarList;

   }


}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

镇原县| 南和县| 大新县| 辰溪县| 图木舒克市| 普洱| 衡南县| 海兴县| 永城市| 达尔| 睢宁县| 三亚市| 安塞县| 全椒县| 汝阳县| 黑山县| 旌德县| 清远市| 汪清县| 五峰| 昆山市| 武川县| 珠海市| 莒南县| 镇平县| 临泉县| 赤城县| 绥江县| 福泉市| 盐池县| 阿图什市| 乌鲁木齐县| 临漳县| 武邑县| 镇雄县| 兴化市| 新津县| 金乡县| 台湾省| 长春市| 阿拉善左旗|