代码之家  ›  专栏  ›  技术社区  ›  Domenico Campagnolo

Apache Flink的ZIP压缩输入

  •  0
  • Domenico Campagnolo  · 技术社区  · 8 年前

    我需要阅读和处理 zip存档中的特定文件 在阿帕奇·弗林克。

    在文档中,我发现

    如果输入文件被标记为适当的文件扩展名,Flink当前支持对这些文件进行透明解压缩。

    https://ci.apache.org/projects/flink/flink-docs-release-1.4/dev/batch/#read-compressed-files

    是否可以在Apache Flink中动态解压缩时处理它?

    2 回复  |  直到 8 年前
        1
  •  1
  •   BrightFlow    8 年前

    这个 FileInputFormat 将读取压缩文件委托给 GZIPInputStream ,它将在解压缩时返回部分解压缩的数据。

        2
  •  0
  •   Domenico Campagnolo    8 年前

    我想分享我同时实现的解决方案。

    所以,在创建了我自己的 InputFormat 我在 open() 方法:

    @Override
    public void open(final FileInputSplit ignored) throws IOException {
        ...
        final XMLInputFactory xmlif = XMLInputFactory.newInstance();
        final XMLStreamReader xmlr = xmlif.createXMLStreamReader(filePath.toString(),
                  InputFormatUtil.readFileWithinZipArchive(filePath, nestedXmlFileName));
        while (xmlr.hasNext()) {
        ...
    }
    

    其中 readFileWithinZipArchive(...) 是:

    public static InputStream readFileWithinZipArchive(final Path zipPath, final String filename) throws IOException {
        // using org.apache.flink.core.fs.Path for getting the InputStream from the (remote) zip archive
        final InputStream zipInputStream = zipPath.getFileSystem().open(zipPath);
        // generating a temporary local copy of the zip file
        final File tmpFile = stream2file(zipInputStream);
        // then using java.util.zip.ZipFile for extracting the InputStream for the specific file within the zip archive
        final ZipFile zipFile = new ZipFile(tmpFile);
        return zipFile.getInputStream(zipFile.getEntry(filename));
    }