Mapper 的输入官方文档如下
The
Mapper
outputs are
sorted and then partitioned
per
Reducer
. The total
number of partitions
is the same as the
number of reduce tasks
for the job. Users can control which keys (and hence records) go to which
Reducer
by implementing a custom
Partitioner
.
这里对map 输出做简单的分析
还是根据官方示例WordCount的示例
第一次分析为了简化map的输出复杂情况,
只分析一个文档,并且其中只有10个'单词',分别为“J", .."c", "b", "a" ( 这里10个字母最好是乱序的,后面会看到其排序),
注释掉设置combine class的代码。
1. 单步跟踪map中的context.write(生产kvbuffer 和kvmeta)
可以追踪到最终实际是由org.apache.hadoop.mapred.MapTask.MapOutputBuffer.collect(K, V, int)
这里因为我们的output 只有10个Record 且每个大小都比较小,所以跳过了spill了处理以及combine处理,主要代码如下,
public synchronized void collect(K key, V value, final int partition
) throws IOException {
) throws IOException {
{
...
keySerializer.serialize(key);
...
valSerializer.serialize(value);
....
kvmeta.put(kvindex + KEYSTART, keystart);
kvmeta.put(kvindex + VALSTART, valstart);
kvmeta.put(kvindex + VALLEN, distanceTo(valstart, valend));
...
}
这里实际是将(K,V) 序列化到了byte数组org.apache.hadoop.mapred.MapTask.MapOutputBuffer.kvbuffer 中,
并将(K,V)在内存中的位置信息 以及 其partition(相同partition的record由同一个reducer处理) 消息 存在 kvmeta 中.
到此map的输出都存在了内存中
2. 通过查找kvmeta的代码索引, 找到消费kvbuffer和kvmeta代码,生产spillRecv到indexCacheList
可以找到在 org.apache.hadoop.mapred.MapTask.MapOutputBuffer.sortAndSpill() 中找到有使用,设置断点,看到如下,
private void sortAndSpill() throws IOException, ClassNotFoundException,
InterruptedException {
InterruptedException {
...
sorter.sort(MapOutputBuffer.this, mstart, mend, reporter);
...
for (int i = 0; i < partitions; ++i) {
...
if (combinerRunner == null) {
// spill directlyDataInputBuffer key = new DataInputBuffer();
while (spindex < mend &&
kvmeta.get(offsetFor(spindex % maxRec) + PARTITION) == i) {
....
writer.append(key, value);
++spindex;
}
}
...
spillRec.putIndex(rec, i);
}
...
indexCacheList.add(spillRec);
...
}
这里有三个操作,
1. Sorter.sort :是以partition 和key 来排序的,目的是聚合相同partition的record, 并以key的顺序排列。
2. writer.append : 将序列化的record 写入输出流,这里写入到文件spill0.out
3. indexCacheList.add : 每个spillRec记录某个spill out文件中包含的partition信息。
3. 查找消费indexCacheList的代码,org.apache.hadoop.mapred.MapTask.MapOutputBuffer.mergeParts()
在此设置断点,可以看到这里我们只有一个spill文件,不需要merge,
这里只是唯一的spillRec 写入到到文件中, file.out.index
将spill0.out 重命名为file.out, 可以vim打开这个文件看到里面存在顺序号的字符。
private void mergeParts() throws IOException, InterruptedException,
ClassNotFoundException {
ClassNotFoundException {
...
sameVolRename(filename[0],
mapOutputFile.getOutputFileForWriteInVolume(filename[0]));
...
indexCacheList.get(0).writeToFile(
mapOutputFile.getOutputIndexFileForWriteInVolume(filename[0]), job);
...
}
总结如下:
1. map的输出首先序列化到内存中kvbuffer,kvmeta
2. sortAndSpill 会将内存中的record写入到文件中这个过程是由另外一个线程来并发执行
3. merge将spill出的文件merge问一个文件file.out,并将每个文件中partition的信息写入file.out.index,这也是为什么K需要支持序列化排序操作的原因
还没分析的情况:
map 输出大量数据,出现多个spill 文件的复杂情况的细节(1. combine 2. 异步spill, 3. merge 多个文件, 这三个步骤涉及的代码比较多不太好列举)
最后总结上图