spark中shuffle过程分区源码分析

spark rdd 在shuffle过程中,涉及数据重组和重新分区,主要是根据key值,把相同的key值分配到同一个区。需要注意的是,因为分区的数量是有限的,所以会有不同的key分到相同的分区,这个过程主要是用hash算法实现。

分区规则由抽象类Partitioner控制。默认分区是用HashPartitioner
在这里插入图片描述
往下找可以找到HashPartitioner

class HashPartitioner(partitions: Int) extends Partitioner {
  require(partitions >= 0, s"Number of partitions ($partitions) cannot be negative.")

  def numPartitions: Int = partitions

  def getPartition(key: Any): Int = key match {
    case null => 0
    //具体的分区方法在nonNegativeMod中
    case _ => Utils.nonNegativeMod(key.hashCode, numPartitions)
  }

  override def equals(other: Any): Boolean = other match {
    case h: HashPartitioner =>
      h.numPartitions == numPartitions
    case _ =>
      false
  }

  override def hashCode: Int = numPartitions
}

ctrl+鼠标左键点击nonNegativeMod

 /* Calculates 'x' modulo 'mod', takes to consideration sign of x,
  * i.e. if 'x' is negative, than 'x' % 'mod' is negative too
  * so function return (x % mod) + mod in that case.
  */
  def nonNegativeMod(x: Int, mod: Int): Int = {
    val rawMod = x % mod
    // 如果hashcode是负值,函数返回(x % mod) + mod
    rawMod + (if (rawMod < 0) mod else 0)
  }

计算过程是:
nonNegativeMod(key.hashCode, numPartitions)
1.参数说明
key.hashCode:每个key值有一个对应的hashCode
numPartitions:分区数

2.key.hashCode % numPartitions 得到余数
这样把key.hashCode值分成numPartitions份,余数就是那一份的id,要注意的是key的hash值也有可能是负数,上面也给出了得到负数时的分区的计算方法。

3.这样就能够根据key值,把相同的key放到同一个分区中

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值