fileinputstream读取不完整_MapReduce中是如何处理相邻来个block块中存储数据不完整的问题的?...

9964f35ac3be70c3177ed974be28f753.png

大家好,今天我们来聊一聊,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 就不读取第一行数据信息;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值