(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks

     

public class HashPartitioner<K2, V2> implements Partitioner<K2, V2> {
    public HashPartitioner() {
    }

    public void configure(JobConf job) {
    }

    public int getPartition(K2 key, V2 value, int numReduceTasks) {
        return (key.hashCode() & 2147483647) % numReduceTasks;
    }
}

 

在mapreduce中 分区过程中通过类HashPartitioner<K,V> implements Partitioner<K,V>  来实现相同的key 的value 会分配到同一个reduceTask中

HashPartitioner 类就是负责处理分配的类

   (key.hashCode() & 2147483647) % numReduceTasks

hasCode()是返回一个哈希值,hashCode()源码是:

 

 public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

返回值是int类型,Integer.MIN_VALUE=2147483647   转换成二进制值是1111111111111111111111111111111

key.hashCode() & 2147483647  可以保证结果是非负数,HashPartitioner类返回的int值就是一个逻辑的分区数值

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值