我必须经常归档 HDFS 文件。这些文件必须使用 Java 代码以 Bunzip 格式压缩。现在,我所做的是:
- 将输入文件移动到本地位置
hdfs.moveToLocalFile
- bzip 使用
bzip2
命令。 - 将 .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/