java—如何压缩存储的文件

p1tboqfb  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(340)

我必须经常存档hdfs文件。这些文件必须用java代码压缩成bunzip格式。现在,我所做的是:
将输入文件移到本地位置 hdfs.moveToLocalFile bzip使用 bzip2 命令。
将.bz2文件移到hdfs的另一个位置 hdfs.moveFromLocalFile .
我使用的是hadoop1.1.2版本。是否有任何api可用于直接bzip文件,而无需本地副本和bzip?
现在我还使用linuxshell命令来压缩文件。有人能帮助我如何使用java代码执行bzip命令吗?

axkjgtzd

axkjgtzd1#

public void addFile(String source, String destination, Configuration paramConfiguration) throws IOException, URISyntaxException {
    FileSystem localFileSystem = FileSystem.get(paramConfiguration);
    String str1 = paramString1.substring(source.lastIndexOf('/') + 1, source.length());
    if (destination.charAt(destination.length() - 1) != '/') {
        destination = destination + "/" + str1;
    } else {
        destination = destination + str1;
    }
    BZip2Codec localBZip2Codec = new BZip2Codec();
    String str2 = localBZip2Codec.getDefaultExtension();
    Path localPath = new Path(paramString2 + str2);

    CompressionOutputStream localCompressionOutputStream = localBZip2Codec.createOutputStream(localFileSystem.create(localPath));

    IOUtils.copyBytes(localFileSystem.open(new Path(paramString1)), localCompressionOutputStream, 4096, true);
}

相关问题