【问题标题】:How can I using Hadoop job to check the integrity of a large gzip file(.gz)?如何使用 Hadoop 作业检查大型 gzip 文件 (.gz) 的完整性?
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

    • 并行调用 gzip -t 似乎是个好办法,但它需要一个本地文件路径,并且在 Hadoop 作业中,您读取的 mapper 中的文件可能不在本地 fs 中,但可能从远程机器,所以不可行。
    • Hadoop 中必须有一个 API 可以为您提供 InputStream(或另一种分块读取文件的方式)。用那个。此外,如果文件在远程机器上,您绝对必须在同一个 CPU 上运行作业 - 通过网络复制文件的成本非常高。
    • 我还建议您在将损坏的数据上传到 Hadoop 之前移动“检查”步骤。 Hadoop 使在集群上分发作业变得容易,但它并没有降低成本——事实上,Hadoop 比在本地处理数据要慢得多。但由于它可以同时运行多个作业,因此您可以平衡两个目标。如果可以,请在上传文件时检查文件,这样您甚至不会浪费磁盘空间来存放损坏的文件。
    • 检查文件比上传快很多倍,所以检查不再是瓶颈。
    • 我正在使用 GNU Parallel 上传文件,因此将它们放入 HDFS 并不慢。相反,本地 gzip -t 验证确实很慢。在我的环境中,使用 GUN Parallel 放文件 + 使用 Hadoop 作业验证的方式每天至少节省 5 个小时。我们只是因为无法容忍本地作业的低效率,所以我们转向了 Hadoop 方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多