2

EMRローカルファイルシステムからs3にzipファイルとしてディレクトリをアップロードしたいと思います。

私が現在使用している方法よりも、これにアプローチするためのより良い方法はありますか?

ZipOutputStreamをReducer出力として返すことは可能でしょうか?

ありがとう

zipFolderAndUpload("target", "target.zip", "s3n://bucketpath/");


static public void zipFolderAndUpload(String srcFolder, String zipFile, String dst) throws Exception {

    //Zips a directory
    FileOutputStream fileWriter = new FileOutputStream(zipFile);
    ZipOutputStream zip = new ZipOutputStream(fileWriter);
    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();

    // Copies the zipped file to the s3 filesystem,
    InputStream in = new BufferedInputStream(new FileInputStream(zipFile));
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(dst+zip), conf);
    OutputStream out = fs.create(new Path(dst+zip));
    IOUtils.copyBytes(in, out, 4096, true);

}

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
        if (path.equals("")) {
            addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
        } else {
            addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
        }
    }
}
4

1 に答える 1

4

あなたが取っているアプローチはうまく見えます。シングルスレッドであるために遅すぎることがわかった場合はOutputFormat、zip ファイルに書き込む独自の Hadoop 実装を作成できます。

注意しなければならないことの 1 つは、Java SE の ZipOutputFormat の実装が Zip64 をサポートしていないことです。つまり、サイズが 4GB を超える ZIP ファイルをサポートしていません。TrueZIP のように、ZIP の他の Java 実装があります。

于 2011-01-31T14:52:18.333 に答える