amazon-s3 - 将目录作为压缩文件从 Elastic MapReduce 上传到 S3

标签 amazon-s3 zip hadoop amazon-emr

我想将 EMR 本地文件系统中的目录作为压缩文件上传到 s3。

是否有比我当前使用的方法更好的方法来解决这个问题?

是否可以返回 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);
        }
    }
}

最佳答案

您正在采取的方法看起来不错。如果您发现它因为是单线程而太慢,那么您可以创建自己的 Hadoop OutputFormat 实现来写入 zip 文件。

您必须注意的一件事是,Java SE 的 ZipOutputFormat 实现不支持 Zip64,这意味着它不支持大小超过 4GB 的 ZIP 文件。 ZIP 的其他 Java 实现也可以这样做,例如 TrueZIP。

关于amazon-s3 - 将目录作为压缩文件从 Elastic MapReduce 上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846555/

相关文章:

hadoop - Spark SQL(1.1.0版本)是否支持hive索引?

Hadoop 多项作业 - 它不会退出 - 需要 Ctrl + C

java - 将电子邮件放入动态 s3 存储桶 Amazon 的收件人规则集

ios - Swift ios 尝试将文件上传到 s3 但文件永远不会上传

Java:处理输入流中的大文件

shell - 如何使用 Cronjob 查看 shell 脚本执行

python - Boto3 没有将 zip 文件上传到 S3 python

django - Firefox WebFont 403 尽管有 S3 CORS 规则

java - 压缩后不保留文件权限

c# - 如何在UWP中解压文件