Java 中的文件压缩 (Hadoop DefaultCodec) - 如何使其可读?

File Compression in Java (Hadoop DefaultCodec) - how to make it human readable?

我有一个文件是用 org.apache.hadoop.io.compress.DefaultCodec 压缩的。我想将此文件恢复为原始格式 - 这是一个 JSON 格式的字符串。

我不太确定如何使用 DefaultCodec 的 documentation 来实现这一点。有人可以给我举个例子吗?这是我目前所知道的,我不知道我是否在正确的轨道上......

//grab my file (it's on S3)
S3Object fileOnS3 = s3Service.getObject("mys3bucket", "myfilename");

DefaultCodec codec = new DefaultCodec();
Decompressor decompressor = codec.createDecompressor();

//does the following line create a input stream that parses DefaultCodec into uncompressed form?
CompressionInputStream is = codec.createInputStream(fileOnS3.getDataInputStream(), decompressor);
//also, I have no idea what to do from here.

我想将未压缩的版本存储在一个 String 变量中,因为我知道该文件是一个小的单行文件。

我会尝试以下方法:

  1. 使用hdfs shell命令-text和unix shell解压文件,像这样:
    hadoop dfs -text /path/on/hdfs/ > /local/path/for/local/raw/file
  2. 使用恒等映射器(和零缩减器)将 SequenceFileInputFormat 用于输入并设置为输出 TextOutputFormat 来加载文件。

我会选择第一个选项,尤其是当您说输入文件是一个小字符串时。如果你想在一个字符串变量中加载这个文件,你可以加载文件(这看起来不必要地昂贵),或者立即将 -text 命令的输出存储在一个字符串中(跳过 > 之后的部分) ).