Map/Reduce之间的Partitioner接口

一、Partitioner介绍

Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是按照Reduce的个数,将Mapper产生的中间结果按照关键字送给不同的Reduce,Reduce对相同关键字的数据进行处理)。

Partitioner在Map/Reduce中所处的位置,如下:

二、Partitioner的源代码解析

将相同关键字Key送到哪个Reduce上处理。

 1 public abstract class Partitioner<KEY, VALUE> {
 2   
 3   /** 
 4    * Get the partition number for a given key (hence record) given the total 
 5    * number of partitions i.e. number of reduce-tasks for the job.
 6    * 通过给定总的分区数(即一般为Reduce的个数),获得每个关键字Key所对应的分区(所对应的Reduce上)。
 7    * <p>Typically a hash function on a all or a subset of the key.</p>
 8    *
 9    * @param key the key to be partioned. 关键字
10    * @param value the entry value.
11    * @param numPartitions the total number of partitions. 一般是Reduce的个数
12    * @return the partition number for the <code>key</code>. 哪个Reduce
13    */
14   public abstract int getPartition(KEY key, VALUE value, int numPartitions);
15   
16 }

 三、常用的Partitioner方法

1、HashPartitioner

HashPartitioner是MapReduce中Partitioner的默认实现。他是基于哈希值的分片方法。实现如下:

 1 public class HashPartitioner<K, V> extends Partitioner<K, V> {
 2 
 3     /** Use {@link Object#hashCode()} to partition. 
 4      * key.hashCode()得到关键字Key的哈希值,numReduceTasks为Reduce的个数
 5      * 这样可以将相同关键字Key的所有数据送给哪个Reduce
 6      **/
 7     public int getPartition(K key, V value, int numReduceTasks) {
 8         return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
 9     }
10 
11 }

2、TotalOrderPartitioner

 TotalOrderPartitioner是基于区间的分片方法,通常用在全排序中。

posted on 2014-05-02 21:10  月下美妞1314  阅读(365)  评论(0编辑  收藏  举报