大家好,今天我们来聊一聊,MapReduce中是如何处理相邻来个block块中存储数据不完整的问题的?
数据存储不完整?也就是前面的block最后一行存储一部分数据,剩下的一部分数据存在在一下个block中?这是后为了保证数据的完整性,mapreduce是如何处理的呢?
这个问题在mapreduce中是这样来处理的:
首先,在读取数据信息的时候,根据每个block起始的偏移量来判断这个block块是否是要处理数据的第一个block块,(也就是起始偏移量start是否为 0) 如果为0 ,就是读取数据的第一个block,这是需要完整的读取整个block信息,而到数据的偏移量不为0时,也就是不是第一个要处理的block块时,需要将第一行数据交给上一个读取block的操作来执行,这样就将数据读取完整了,且判断偏移量是否为最后一个block块,如果是最后一个,就将该block块信息读取完整即可,无需读取下一个blcok块的首行信息;
MapReduce的对于这个问题的相关源码如下:
public void initialize(InputSplit genericSplit,
TaskAttemptContext context) throws IOException {
FileSplit split = (FileSplit) genericSplit;
Configuration job = context.getConfiguration();
this.maxLineLength = job.getInt(MAX_LINE_LENGTH, Integer.MAX_VALUE);
start = split.getStart();
end = start + split.getLength();
final Path file = split.getPath();
// open the file and seek to the start of the split
final FileSystem fs = file.getFileSystem(job);
fileIn = fs.open(file);
CompressionCodec codec = new CompressionCodecFactory(job).getCodec(file);
if (null!=codec) {
isCompressedInput = true;
decompressor = CodecPool.getDecompressor(codec);
if (codec instanceof SplittableCompressionCodec) {
final SplitCompressionInputStream cIn =
((SplittableCompressionCodec)codec).createInputStream(
fileIn, decompressor, start, end,
SplittableCompressionCodec.READ_MODE.BYBLOCK);
in = new CompressedSplitLineReader(cIn, job,
this.recordDelimiterBytes);
start = cIn.getAdjustedStart();
end = cIn.getAdjustedEnd();
filePosition = cIn;
} else {
in = new SplitLineReader(codec.createInputStream(fileIn,
decompressor), job, this.recordDelimiterBytes);
filePosition = fileIn;
}
} else {
fileIn.seek(start);
in = new UncompressedSplitLineReader(
fileIn, job, this.recordDelimiterBytes, split.getLength());
filePosition = fileIn;
}
// If this is not the first split, we always throw away first record
// because we always (except the last split) read one extra line in
// next() method.
if (start != 0) {
start += in.readLine(new Text(), 0, maxBytesToConsume(start));
}
this.pos = start;
}
其中: if (start != 0) {
start += in.readLine(new Text(), 0, maxBytesToConsume(start));
} 就是判断偏移量是否为0 如果不为0 就不读取第一行数据信息;