繁体   English   中英

如何使用Hadoop作业检查大gzip文件(.gz)的完整性?

[英]How can I using Hadoop job to check the integrity of a large gzip file(.gz)?

我每天都会收到很多来自他人的gzip文件(* .gz),在将它们放入HDFS并进行分析之前,如果我使用gzip -t file_name进行检查,则需要检查所有文件的完整性(损坏的文件将被删除)。在本地计算机上,它可以工作,但是整个过程太慢,因为文件数量非常大,并且大多数文件都足够大,以致于本地验证是一项耗时的工作。

因此,我转而使用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)时,由于Java堆空间问题,Hadoop作业将失败,原因很明显:对于每个文件,我首先收集所有数据放入内存缓冲区,最后进行一次验证,因此文件大小不能太大。

因此,我将部分代码修改为:

  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)将被视为损坏的文件!

所以这是我的问题:如何正确进行验证,避免将单个文件的所有内容读入内存的问题?

任何想法将不胜感激,在此先感谢。

我的第一个解决方案是简单地并行调用gzip -t gzip可能比Java快,并且当文件很大时,创建进程的额外开销应该可以忽略不计。

您的解决方案非常慢。 首先,当每个文件只需要几个KB的数据时,您就将许多GB的数据加载到RAM中。 而不是JustBytesInputFormat ,您应该流式传输数据。 尝试找到一种将InputStream而不是整个文件内容传递给testUnGZip()的方法。

如果该文件作为真实文件存在于硬盘上,请尝试使用NIO API进行读取; 这样可以将文件映射到内存中,从而使读取速度更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM