【发布时间】:2014-12-04 07:48:24
【问题描述】:
我每天都会收到很多来自其他人的gzip文件(*.gz),如果我使用,在将它们放到HDFS并分析它们之前,我需要检查所有文件的完整性(损坏的文件将被删除) gzip -t file_name 在本地机器上检查,可以,但是整个过程太慢,因为文件量很大,而且大多数文件都大到足以使本地验证成为一项耗时的工作。 p>
所以我转而使用 Hadoop 作业进行并行验证,每个文件都将在映射器中进行验证,并将损坏的文件路径输出到文件中,这是我的代码:
在 Hadoop 作业设置中:
Job job = new Job(getConf());
job.setJarByClass(HdfsFileValidateJob.class);
job.setMapperClass(HdfsFileValidateMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setNumReduceTasks(0);
job.setInputFormatClass(JustBytesInputFormat.class);
在映射器中:
public class HdfsFileValidateMapper extends Mapper<JustBytesWritable, NullWritable, Text, NullWritable> {
private static final Logger LOG = LoggerFactory.getLogger(HdfsFileValidateJob.class);
private ByteArrayOutputStream bos;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
/* specify a split size(=HDFS block size here) for the ByteArrayOutputStream, which prevents frequently allocating
* memory for it when writing data in [map] method */
InputSplit inputSplit = context.getInputSplit();
bos = new ByteArrayOutputStream((int) ((FileSplit) inputSplit).getLength());
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
InputSplit inputSplit = context.getInputSplit();
String filePath = ((FileSplit) inputSplit).getPath().toUri().getPath(); // e.g. "/user/hadoop/abc.txt"
bos.flush();
byte[] mergedArray = bos.toByteArray(); // the byte array which stores the data of the whole file
if (!testUnGZip(mergedArray)) { // broken file
context.write(new Text(filePath), NullWritable.get());
}
bos.close();
}
@Override
public void map(JustBytesWritable key, NullWritable value, Context context) throws IOException, InterruptedException {
bos.write(key.getBytes());
}
/**
* Test whether we can un-gzip a piece of data.
*
* @param data The data to be un-gzipped.
* @return true for successfully un-gzipped the data, false otherwise.
*/
private static boolean testUnGZip(byte[] data) {
int numBytes2Read = 0;
ByteArrayInputStream bis = null;
GZIPInputStream gzip = null;
try {
bis = new ByteArrayInputStream(data);
gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num;
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
numBytes2Read += num;
if (numBytes2Read % (1024 * 1024) == 0) {
LOG.info(String.format("Number of bytes read: %d", numBytes2Read));
}
}
} catch (Exception e) {
return false;
} finally {
if (gzip != null) {
try {
gzip.close();
} catch (IOException e) {
LOG.error("Error while closing GZIPInputStream");
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
LOG.error("Error while closing ByteArrayInputStream");
}
}
}
return true;
}
}
我在其中使用了两个名为 JustBytesInputFormat 和 JustBytesWritable 的类,可以在这里找到: https://issues.apache.org/jira/secure/attachment/12570327/justbytes.jar
通常情况下,这个解决方案可以正常工作,但是当单个 gzip 文件足够大(例如 1.5G)时,Hadoop 作业会由于 Java 堆空间问题而失败,原因很明显:对于我首先收集的每个文件将所有数据放入内存缓冲区,最后进行一次性验证,文件大小不能太大。
所以我将部分代码修改为:
private boolean testUnGzipFail = false;
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
InputSplit inputSplit = context.getInputSplit();
String filePath = ((FileSplit) inputSplit).getPath().toUri().getPath(); // e.g. "/user/hadoop/abc.txt"
if (testUnGzipFail) { // broken file
context.write(new Text(filePath), NullWritable.get());
}
}
@Override
public void map(JustBytesWritable key, NullWritable value, Context context) throws IOException, InterruptedException {
if (!testUnGZip(key.getBytes())) {
testUnGzipFail = true;
}
}
这个版本修复了 Hadoop 作业失败的问题,但是 它根本不能正常工作!在我的 E2E 测试中,一个完全正常的 gzip 文件(大小:1.5G)将被视为损坏文件!
所以这是我的问题: 如何正确进行验证,避免将单个文件的所有内容读入内存的问题?
任何想法将不胜感激,在此先感谢。
【问题讨论】:
标签: java validation hadoop gzip integrity