Mapper/Reducer:
void map(K1 key, V1 value, OutputCollector<K2, V2> output, Reporter reporter)
throws IOException;
void reduce(K2 key, Iterator<V2> values,OutputCollector<K3, V3> output, Reporter reporter)
throws IOException;
map()为例:
MapReduce框架会通过InputFormat中RecordReader从InputSplit获取一个个Key Value对,并交由map()函数处理。
Partitioner:
作用是对mapper产生的中间结果进行分片,将同一分组的数据交给同一个Reducer处理。第三个参数便是每个mapper的分片数,即Reducer个数。
int getPartition(K2 key, V2 value, int numPartitions);
如何根据Reducer个数对数据进行分组呢??
HashPartitioner(hadoop默认实现方法):
public int getPartition(K2 key, V2 value,
int numReduceTasks) {
return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
TotalOrderPartitioner(经典):
用于对数据的全排序中,如果数据量很大,只有一个Reducer会成为作业的瓶颈。方法分析:
1、首先进行数据采样(依赖于好的采样算法),然后将采样进行排序;
2、根据设定的Reduce Task个数N,对排完序的采样数据N等分,确定分割点;
3、将以上的分割点保存到Trie树中,如此一来,就将输入数据分为N等分;
4、由于每个partition有序,对N等分中的局部数据进行排序,即完成了全排序的数据结果。