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是基于区间的分片方法,通常用在全排序中。

转载于:https://www.cnblogs.com/rolly-yan/p/3704448.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值