在gzip文件上使用CombineFileInputFormat的最佳方法是什么?
本文将帮助您在CombineFIleInputFOrmat的帮助下构建自己的Inputformat来读取和处理gzip文件。下面的部分会给你一个需要做什么的想法。
定制InputFormat:构建您自己的自定义组合文件输入格式,几乎与组合文件输入格式相同。Key必须是我们自己的可写类,它将保存文件名,偏移量和值将是实际的文件内容。必须将issplittable设置为false(我们不想分割文件)。将maxsplitsize设置为您需要的值。根据该值,Combinefilerecordreader决定分割的数量,并为每个分割创建一个实例。您必须通过添加您的解压缩逻辑来构建您自己的自定义recordreader。
定制RecordReader:Custom Recordreader使用lineereader并将键设置为文件名,偏移量和值设置为实际文件内容。如果文件被压缩,它将解压缩并读取它。
private void codecWiseDecompress(Configuration conf) throws IOException{
CompressionCodecFactory factory = new CompressionCodecFactory(conf);
CompressionCodec codec = factory.getCodec(path);
if (codec == null) {
System.err.println("No Codec Found For " + path);
System.exit(1);
}
String outputUri =
CompressionCodecFactory.removeSuffix(path.toString(),
codec.getDefaultExtension());
dPath = new Path(outputUri);
InputStream in = null;
OutputStream out = null;
fs = this.path.getFileSystem(conf);
try {
in = codec.createInputStream(fs.open(path));
out = fs.create(dPath);
IOUtils.copyBytes(in, out, conf);
} finally {
IOUtils.closeStream(in);
IOUtils.closeStream(out);
rlength = fs.getFileStatus(dPath).getLen();
}
}
自定义可写类:
文件名,偏移值