java - 如何打包存储的文件

标签 java hadoop hdfs bzip2

我必须经常归档 HDFS 文件。这些文件必须使用 Java 代码以 Bunzip 格式压缩。现在,我所做的是:

  1. 将输入文件移动到本地位置hdfs.moveToLocalFile
  2. bzip 使用 bzip2 命令。
  3. 将 .bz2 文件移动到 HDFS 的另一个位置hdfs.moveFromLocalFile

我使用的是 Hadoop 1.1.2 版本。是否有任何 API 可用于直接 bzip 文件,而无需本地副本和 BZip?

另外,现在我正在使用 linux shell 命令对文件进行 BZip 压缩。有人可以帮助我如何使用 Java 代码执行 BZip 命令吗?

最佳答案

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);
}

关于java - 如何打包存储的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23604371/

相关文章:

Hadoop dfs 复制

mysql - 在 HIVE 插入查询中,为什么 from 子句在开头?

hadoop - 尝试删除数据库时出错

azure - 将 Azure blob 与 Hadoop 结合使用时配置 defaultFS

Java进程waitFor()函数导致应用程序卡住

java - 从相机获得更高质量的图片

java - 在 java 帮助中格式化输出

java - 编译 GWT 项目时出现内存不足异常

Hadoop HDFS 没有均匀分布数据 block

java - 在Java中访问HDFS文件系统的异常