对大文件使用自定义recordreader时出现java堆错误

jhkqcmku  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(310)

我已经编写了一个自定义的文件读取器来不分割我的输入文件,因为它们是大的gzip文件,我希望我的第一个Map程序工作就是简单地对它们进行gunzip压缩。我遵循了《hadoop权威指南》中的示例,但在尝试读入byteswriteable时遇到了堆错误。我相信这是因为字节数组的大小是85713669,但我不知道如何克服这个问题。
代码如下:

public class WholeFileRecordReader extends RecordReader<NullWritable, BytesWritable> {

private FileSplit fileSplit;
private Configuration conf;
private BytesWritable value = new BytesWritable();
private boolean processed = false;

@Override
public void close() throws IOException {
    // do nothing
}

@Override
public NullWritable getCurrentKey() throws IOException,
        InterruptedException {
    return NullWritable.get();
}

@Override
public BytesWritable getCurrentValue() throws IOException,
        InterruptedException {
    return value;
}

@Override
public float getProgress() throws IOException, InterruptedException {
    return processed ? 1.0f : 0.0f;
}

@Override
public void initialize(InputSplit split, TaskAttemptContext context)
        throws IOException, InterruptedException {
    this.fileSplit = (FileSplit) split;
    this.conf = context.getConfiguration();
}

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (!processed) {
        byte[] contents = new byte[(int) fileSplit.getLength()];
        Path file = fileSplit.getPath();
        FileSystem fs = file.getFileSystem(conf);
        FSDataInputStream in = null;
        try {
            in = fs.open(file);
            IOUtils.readFully(in, contents, 0, contents.length);
            value.set(contents, 0, contents.length);
        } finally {
            IOUtils.closeStream(in);
        }
        processed = true;
        return true;
    }
    return false;
}

}

z9zf31ra

z9zf31ra1#

一般来说,您不能将整个文件加载到javavm的内存中。您应该找到一些流解决方案来处理大文件—逐块读取数据并将结果保存在内存中,而不固定整个数据集
这种特定的任务解压可能不适合mr,因为没有将数据按逻辑划分为记录。
还请注意,hadoop正在自动处理gzip—您的输入流将已经解压缩。

相关问题