Spark分区方式详解

一、Spark数据分区方式简要      

       在Spark中,RDD(Resilient Distributed Dataset)是其最基本的抽象数据集,其中每个RDD是由若干个Partition组成。在Job运行期间,参与运算的Partition数据分布在多台机器的内存当中。这里可将RDD看成一个非常大的数组,其中Partition是数组中的每个元素,并且这些元素分布在多台机器中。图一中,RDD1包含了5个Partition,RDD2包含了3个Partition,这些Partition分布在4个节点中。

        Spark包含两种数据分区方式:HashPartitioner(哈希分区)和RangePartitioner(范围分区)。一般而言,对于初始读入的数据是不具有任何的数据分区方式的。数据分区方式只作用于<Key,Value>形式的数据。因此,当一个Job包含Shuffle操作类型的算子时,如groupByKey,reduceByKey etc,此时就会使用数据分区方式来对数据进行分区,即确定某一个Key对应的键值对数据分配到哪一个Partition中。在Spark Shuffle阶段中,共分为Shuffle Write阶段和Shuffle Read阶段,其中在Shuffle Write阶段中,Shuffle Map Task对数据进行处理产生中间数据,然后再根据数据分区方式对中间数据进行分区。最终Shffle Read阶段中的Shuffle Read Task会拉取Shuffle Write阶段中产生的并已经分好区的中间数据。图2中描述了Shuffle阶段与Partition关系。下面则分别介绍Spark中存在的两种数据分区方式。

二、HashPartitioner(哈希分区)

1、HashPartitioner原理简介

      HashPartitioner采用哈希的方式对<Key,Value>键值对数据进行分区。其数据分区规则为 partitionId = Key.hashCode % numPartitions,其中partitionId代表该Key对应的键值对数据应当分配到的Partition标识,Key.hashCode表示该Key的哈希值,numPartitions表示包含的Partition个数。图3简单描述了HashPartitioner的数据分区过程。

2、HashPartitioner源码详解

        HashPartitioner源码较为简单,这里不再进行详细解释。

  1. class HashPartitioner(partitions: Int) extends Partitioner {
  2. require(partitions >= 0, s"Number of partitions ($partitions) cannot be negative.")
  3. /**
  4. * 包含的分区个数
  5. */
  6. def numPartitions: Int = partitions
  7. /**
  8. * 获得Key对应的partitionId
  9. */
  10. def getPartition(key: Any): Int = key match {
  11. case null => 0
  12. case _ => Utils.nonNegativeMod(key.hashCode, numPartitions)
  13. }
  14. override def equals(other: Any): Boolean = other match {
  15. case h: HashPartitioner =>
  16. h.numPartitions == numPartitions
  17. case _ =>
  18. false
  19. }
  20. override def hashCode: Int = numPartitions
  21. }
  22. def nonNegativeMod(x: Int, mod: Int): Int = {
  23. val rawMod = x % mod
  24. rawMod + (if (rawMod < 0) mod else 0)
  25. }

三、RangePartitioner(范围分区)

1、RangePartitioner原理简介     

      Spark引入RangePartitioner的目的是为了解决HashPartitioner所带来的分区倾斜问题,也即分区中包含的数据量不均衡问题。HashPartitioner采用哈希的方式将同一类型的Key分配到同一个Partition中,因此当某一或某几种类型数据量较多时,就会造成若干Partition中包含的数据过大问题,而在Job执行过程中,一个Partition对应一个Task,此时就会使得某几个Task运行过慢。RangePartitioner基于抽样的思想来对数据进行分区。图4简单描述了RangePartitioner的数据分区过程。

 2、RangePartitioner源码详解

① 确定采样数据的规模:RangePartitioner默认对生成的子RDD中的每个Partition采集20条数据,样本数据最大为1e6条。

  1. // 总共需要采集的样本数据个数,其中partitions代表最终子RDD中包含的Partition个数
  2. val sampleSize = math.min(20.0 * partitions, 1e6)

② 确定父RDD中每个Partition中应当采集的数据量:这里注意的是,对父RDD中每个Partition采集的数据量会在平均值上乘以3,这里是为了后继在进行判断一个Partition是否发生了倾斜,当一个Partition包含的数据量超过了平均值的三倍,此时会认为该Partition发生了数据倾斜,会对该Partition调用sample算子进行重新采样。

  1. // 被采样的RDD中每个partition应该被采集的数据,这里将平均采集每个partition中数据的3倍
  2. val sampleSizePerPartition = math.ceil(3.0 * sampleSize / rdd.partitions.length).toInt

③ 调用sketch方法进行数据采样:sketch方法返回的结果为<采样RDD的数据量,<partitionId, 分区数据量,分区采样的数据量>>。在sketch方法中会使用水塘抽样算法对待采样的各个分区进行数据采样,这里采用水塘抽样算法是由于实现无法知道每个Partition中包含的数据量,而水塘抽样算法可以保证在不知道整体的数据量下仍然可以等概率地抽取出每条数据。图4简单描述了水塘抽样过程。

  1. // 使用sketch方法进行数据抽样
  2. val (numItems, sketched) = RangePartitioner.sketch(rdd.map(_._1), sampleSizePerPartition)
  3. /**
  4. * @param rdd 需要采集数据的RDD
  5. * @param sampleSizePerPartition 每个partition采集的数据量
  6. * @return <采样RDD数据总量,<partitionId, 当前分区的数据量,当前分区采集的数据量>>
  7. */
  8. def sketch[K : ClassTag](
  9. rdd: RDD[K],
  10. sampleSizePerPartition: Int): (Long, Array[(Int, Long, Array[K])]) = {
  11. val shift = rdd.id
  12. val sketched = rdd.mapPartitionsWithIndex { (idx, iter) =>
  13. val seed = byteswap32(idx ^ (shift << 16))
  14. // 使用水塘抽样算法进行抽样,抽样结果是个二元组<Partition中抽取的样本量,Partition中包含的数据量>
  15. val (sample, n) = SamplingUtils.reservoirSampleAndCount(
  16. iter, sampleSizePerPartition, seed)
  17. Iterator((idx, n, sample))
  18. }.collect()
  19. val numItems = sketched.map(_._2).sum
  20. (numItems, sketched)
  21. }

④ 数据抽样完成后,需要对不均衡的Partition重新进行抽样,默认当Partition中包含的数据量大于平均值的三倍时,该Partition是不均衡的。当采样完成后,利用样本容量和RDD中包含的数据总量,可以得到整体的一个数据采样率fraction。利用此采样率对不均衡的Partition调用sample算子重新进行抽样。

  1. // 计算数据采样率
  2. val fraction = math.min(sampleSize / math.max(numItems, 1L), 1.0)
  3. // 存放采样Key以及采样权重
  4. val candidates = ArrayBuffer.empty[(K, Float)]
  5. // 存放不均衡的Partition
  6. val imbalancedPartitions = mutable.Set.empty[Int]
  7. //(idx, n, sample)=> (partition id, 当前分区数据个数,当前partition的采样数据)
  8. sketched.foreach { case (idx, n, sample) =>
  9. // 当一个分区中的数据量大于平均分区数据量的3倍时,认为该分区是倾斜的
  10. if (fraction * n > sampleSizePerPartition) {
  11. imbalancedPartitions += idx
  12. }
  13. // 在三倍之内的认为没有发生数据倾斜
  14. else {
  15. // 每条数据的采样间隔 = 1/采样率 = 1/(sample.size/n.toDouble) = n.toDouble/sample.size
  16. val weight = (n.toDouble / sample.length).toFloat
  17. // 对当前分区中的采样数据,对每个key形成一个二元组<key, weight>
  18. for (key <- sample) {
  19. candidates += ((key, weight))
  20. }
  21. }
  22. }
  23. // 对于非均衡的partition,重新采用sample算子进行抽样
  24. if (imbalancedPartitions.nonEmpty) {
  25. val imbalanced = new PartitionPruningRDD(rdd.map(_._1), imbalancedPartitions.contains)
  26. val seed = byteswap32(-rdd.id - 1)
  27. val reSampled = imbalanced.sample(withReplacement = false, fraction, seed).collect()
  28. val weight = (1.0 / fraction).toFloat
  29. candidates ++= reSampled.map(x => (x, weight))
  30. }

⑤ 确定各个Partition的Key范围:使用determineBounds方法来确定每个Partition中包含的Key范围,先对采样的Key进行排序,然后计算每个Partition平均包含的Key权重,然后采用平均分配原则来确定各个Partition包含的Key范围。如当前采样Key以及权重为:<1, 0.2>, <2, 0.1>, <3, 0.1>, <4, 0.3>, <5, 0.1>, <6, 0.3>,现在将其分配到3个Partition中,则每个Partition的平均权重为:(0.2 + 0.1 + 0.1 + 0.3 + 0.1 + 0.3) / 3 = 0.36。此时Partition1 ~ 3分配的Key以及总权重为<Partition1, {1, 2, 3}, 0.4> <Partition2, {4, 5}, 0.4> <Partition1, {6}, 0.3>。

  1. /**
  2. * @param candidates 未按采样间隔排序的抽样数据
  3. * @param partitions 最终生成的RDD包含的分区个数
  4. * @return 分区边界
  5. */
  6. def determineBounds[K : Ordering : ClassTag](
  7. candidates: ArrayBuffer[(K, Float)],
  8. partitions: Int): Array[K] = {
  9. val ordering = implicitly[Ordering[K]]
  10. // 对样本按照key进行排序
  11. val ordered = candidates.sortBy(_._1)
  12. // 抽取的样本容量
  13. val numCandidates = ordered.size
  14. // 抽取的样本对应的采样间隔之和
  15. val sumWeights = ordered.map(_._2.toDouble).sum
  16. // 平均每个分区的步长
  17. val step = sumWeights / partitions
  18. var cumWeight = 0.0
  19. var target = step
  20. // 分区边界值
  21. val bounds = ArrayBuffer.empty[K]
  22. var i = 0
  23. var j = 0
  24. var previousBound = Option.empty[K]
  25. while ((i < numCandidates) && (j < partitions - 1)) {
  26. val (key, weight) = ordered(i)
  27. cumWeight += weight
  28. // 当前的采样间隔小于target,继续迭代,也即这些key应该放在同一个partition中
  29. if (cumWeight >= target) {
  30. // Skip duplicate values.
  31. if (previousBound.isEmpty || ordering.gt(key, previousBound.get)) {
  32. bounds += key
  33. target += step
  34. j += 1
  35. previousBound = Some(key)
  36. }
  37. }
  38. i += 1
  39. }
  40. bounds.toArray
  41. }

⑥ 计算每个Key所在Partition:当分区范围长度在128以内,使用顺序搜索来确定Key所在的Partition,否则使用二分查找算法来确定Key所在的Partition。

  1. /**
  2. * 获得每个Key所在的partitionId
  3. */
  4. def getPartition(key: Any): Int = {
  5. val k = key.asInstanceOf[K]
  6. var partition = 0
  7. // 如果得到的范围不大于128,则进行顺序搜索
  8. if (rangeBounds.length <= 128) {
  9. // If we have less than 128 partitions naive search
  10. while (partition < rangeBounds.length && ordering.gt(k, rangeBounds(partition))) {
  11. partition += 1
  12. }
  13. }
  14. // 范围大于128,则进行二分搜索该key所在范围,即可得到该key所在的partitionId
  15. else {
  16. // Determine which binary search method to use only once.
  17. partition = binarySearch(rangeBounds, k)
  18. // binarySearch either returns the match location or -[insertion point]-1
  19. if (partition < 0) {
  20. partition = -partition-1
  21. }
  22. if (partition > rangeBounds.length) {
  23. partition = rangeBounds.length
  24. }
  25. }
  26. if (ascending) {
  27. partition
  28. } else {
  29. rangeBounds.length - partition
  30. }
  31. }
                    <h1><a name="t0"></a>一、Spark数据分区方式简要 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h1>

       在Spark中,RDD(Resilient Distributed Dataset)是其最基本的抽象数据集,其中每个RDD是由若干个Partition组成。在Job运行期间,参与运算的Partition数据分布在多台机器的内存当中。这里可将RDD看成一个非常大的数组,其中Partition是数组中的每个元素,并且这些元素分布在多台机器中。图一中,RDD1包含了5个Partition,RDD2包含了3个Partition,这些Partition分布在4个节点中。

        Spark包含两种数据分区方式:HashPartitioner(哈希分区)和RangePartitioner(范围分区)。一般而言,对于初始读入的数据是不具有任何的数据分区方式的。数据分区方式只作用于<Key,Value>形式的数据。因此,当一个Job包含Shuffle操作类型的算子时,如groupByKey,reduceByKey etc,此时就会使用数据分区方式来对数据进行分区,即确定某一个Key对应的键值对数据分配到哪一个Partition中。在Spark Shuffle阶段中,共分为Shuffle Write阶段和Shuffle Read阶段,其中在Shuffle Write阶段中,Shuffle Map Task对数据进行处理产生中间数据,然后再根据数据分区方式对中间数据进行分区。最终Shffle Read阶段中的Shuffle Read Task会拉取Shuffle Write阶段中产生的并已经分好区的中间数据。图2中描述了Shuffle阶段与Partition关系。下面则分别介绍Spark中存在的两种数据分区方式。

二、HashPartitioner(哈希分区)

1、HashPartitioner原理简介

      HashPartitioner采用哈希的方式对<Key,Value>键值对数据进行分区。其数据分区规则为 partitionId = Key.hashCode % numPartitions,其中partitionId代表该Key对应的键值对数据应当分配到的Partition标识,Key.hashCode表示该Key的哈希值,numPartitions表示包含的Partition个数。图3简单描述了HashPartitioner的数据分区过程。

2、HashPartitioner源码详解

        HashPartitioner源码较为简单,这里不再进行详细解释。

  1. class HashPartitioner(partitions: Int) extends Partitioner {
  2. require(partitions >= 0, s"Number of partitions ($partitions) cannot be negative.")
  3. /**
  4. * 包含的分区个数
  5. */
  6. def numPartitions: Int = partitions
  7. /**
  8. * 获得Key对应的partitionId
  9. */
  10. def getPartition(key: Any): Int = key match {
  11. case null => 0
  12. case _ => Utils.nonNegativeMod(key.hashCode, numPartitions)
  13. }
  14. override def equals(other: Any): Boolean = other match {
  15. case h: HashPartitioner =>
  16. h.numPartitions == numPartitions
  17. case _ =>
  18. false
  19. }
  20. override def hashCode: Int = numPartitions
  21. }
  22. def nonNegativeMod(x: Int, mod: Int): Int = {
  23. val rawMod = x % mod
  24. rawMod + (if (rawMod < 0) mod else 0)
  25. }

三、RangePartitioner(范围分区)

1、RangePartitioner原理简介     

      Spark引入RangePartitioner的目的是为了解决HashPartitioner所带来的分区倾斜问题,也即分区中包含的数据量不均衡问题。HashPartitioner采用哈希的方式将同一类型的Key分配到同一个Partition中,因此当某一或某几种类型数据量较多时,就会造成若干Partition中包含的数据过大问题,而在Job执行过程中,一个Partition对应一个Task,此时就会使得某几个Task运行过慢。RangePartitioner基于抽样的思想来对数据进行分区。图4简单描述了RangePartitioner的数据分区过程。

 2、RangePartitioner源码详解

① 确定采样数据的规模:RangePartitioner默认对生成的子RDD中的每个Partition采集20条数据,样本数据最大为1e6条。

  1. // 总共需要采集的样本数据个数,其中partitions代表最终子RDD中包含的Partition个数
  2. val sampleSize = math.min(20.0 * partitions, 1e6)

② 确定父RDD中每个Partition中应当采集的数据量:这里注意的是,对父RDD中每个Partition采集的数据量会在平均值上乘以3,这里是为了后继在进行判断一个Partition是否发生了倾斜,当一个Partition包含的数据量超过了平均值的三倍,此时会认为该Partition发生了数据倾斜,会对该Partition调用sample算子进行重新采样。

  1. // 被采样的RDD中每个partition应该被采集的数据,这里将平均采集每个partition中数据的3倍
  2. val sampleSizePerPartition = math.ceil(3.0 * sampleSize / rdd.partitions.length).toInt

③ 调用sketch方法进行数据采样:sketch方法返回的结果为<采样RDD的数据量,<partitionId, 分区数据量,分区采样的数据量>>。在sketch方法中会使用水塘抽样算法对待采样的各个分区进行数据采样,这里采用水塘抽样算法是由于实现无法知道每个Partition中包含的数据量,而水塘抽样算法可以保证在不知道整体的数据量下仍然可以等概率地抽取出每条数据。图4简单描述了水塘抽样过程。

  1. // 使用sketch方法进行数据抽样
  2. val (numItems, sketched) = RangePartitioner.sketch(rdd.map(_._1), sampleSizePerPartition)
  3. /**
  4. * @param rdd 需要采集数据的RDD
  5. * @param sampleSizePerPartition 每个partition采集的数据量
  6. * @return <采样RDD数据总量,<partitionId, 当前分区的数据量,当前分区采集的数据量>>
  7. */
  8. def sketch[K : ClassTag](
  9. rdd: RDD[K],
  10. sampleSizePerPartition: Int): (Long, Array[(Int, Long, Array[K])]) = {
  11. val shift = rdd.id
  12. val sketched = rdd.mapPartitionsWithIndex { (idx, iter) =>
  13. val seed = byteswap32(idx ^ (shift << 16))
  14. // 使用水塘抽样算法进行抽样,抽样结果是个二元组<Partition中抽取的样本量,Partition中包含的数据量>
  15. val (sample, n) = SamplingUtils.reservoirSampleAndCount(
  16. iter, sampleSizePerPartition, seed)
  17. Iterator((idx, n, sample))
  18. }.collect()
  19. val numItems = sketched.map(_._2).sum
  20. (numItems, sketched)
  21. }

④ 数据抽样完成后,需要对不均衡的Partition重新进行抽样,默认当Partition中包含的数据量大于平均值的三倍时,该Partition是不均衡的。当采样完成后,利用样本容量和RDD中包含的数据总量,可以得到整体的一个数据采样率fraction。利用此采样率对不均衡的Partition调用sample算子重新进行抽样。

  1. // 计算数据采样率
  2. val fraction = math.min(sampleSize / math.max(numItems, 1L), 1.0)
  3. // 存放采样Key以及采样权重
  4. val candidates = ArrayBuffer.empty[(K, Float)]
  5. // 存放不均衡的Partition
  6. val imbalancedPartitions = mutable.Set.empty[Int]
  7. //(idx, n, sample)=> (partition id, 当前分区数据个数,当前partition的采样数据)
  8. sketched.foreach { case (idx, n, sample) =>
  9. // 当一个分区中的数据量大于平均分区数据量的3倍时,认为该分区是倾斜的
  10. if (fraction * n > sampleSizePerPartition) {
  11. imbalancedPartitions += idx
  12. }
  13. // 在三倍之内的认为没有发生数据倾斜
  14. else {
  15. // 每条数据的采样间隔 = 1/采样率 = 1/(sample.size/n.toDouble) = n.toDouble/sample.size
  16. val weight = (n.toDouble / sample.length).toFloat
  17. // 对当前分区中的采样数据,对每个key形成一个二元组<key, weight>
  18. for (key <- sample) {
  19. candidates += ((key, weight))
  20. }
  21. }
  22. }
  23. // 对于非均衡的partition,重新采用sample算子进行抽样
  24. if (imbalancedPartitions.nonEmpty) {
  25. val imbalanced = new PartitionPruningRDD(rdd.map(_._1), imbalancedPartitions.contains)
  26. val seed = byteswap32(-rdd.id - 1)
  27. val reSampled = imbalanced.sample(withReplacement = false, fraction, seed).collect()
  28. val weight = (1.0 / fraction).toFloat
  29. candidates ++= reSampled.map(x => (x, weight))
  30. }

⑤ 确定各个Partition的Key范围:使用determineBounds方法来确定每个Partition中包含的Key范围,先对采样的Key进行排序,然后计算每个Partition平均包含的Key权重,然后采用平均分配原则来确定各个Partition包含的Key范围。如当前采样Key以及权重为:<1, 0.2>, <2, 0.1>, <3, 0.1>, <4, 0.3>, <5, 0.1>, <6, 0.3>,现在将其分配到3个Partition中,则每个Partition的平均权重为:(0.2 + 0.1 + 0.1 + 0.3 + 0.1 + 0.3) / 3 = 0.36。此时Partition1 ~ 3分配的Key以及总权重为<Partition1, {1, 2, 3}, 0.4> <Partition2, {4, 5}, 0.4> <Partition1, {6}, 0.3>。

  1. /**
  2. * @param candidates 未按采样间隔排序的抽样数据
  3. * @param partitions 最终生成的RDD包含的分区个数
  4. * @return 分区边界
  5. */
  6. def determineBounds[K : Ordering : ClassTag](
  7. candidates: ArrayBuffer[(K, Float)],
  8. partitions: Int): Array[K] = {
  9. val ordering = implicitly[Ordering[K]]
  10. // 对样本按照key进行排序
  11. val ordered = candidates.sortBy(_._1)
  12. // 抽取的样本容量
  13. val numCandidates = ordered.size
  14. // 抽取的样本对应的采样间隔之和
  15. val sumWeights = ordered.map(_._2.toDouble).sum
  16. // 平均每个分区的步长
  17. val step = sumWeights / partitions
  18. var cumWeight = 0.0
  19. var target = step
  20. // 分区边界值
  21. val bounds = ArrayBuffer.empty[K]
  22. var i = 0
  23. var j = 0
  24. var previousBound = Option.empty[K]
  25. while ((i < numCandidates) && (j < partitions - 1)) {
  26. val (key, weight) = ordered(i)
  27. cumWeight += weight
  28. // 当前的采样间隔小于target,继续迭代,也即这些key应该放在同一个partition中
  29. if (cumWeight >= target) {
  30. // Skip duplicate values.
  31. if (previousBound.isEmpty || ordering.gt(key, previousBound.get)) {
  32. bounds += key
  33. target += step
  34. j += 1
  35. previousBound = Some(key)
  36. }
  37. }
  38. i += 1
  39. }
  40. bounds.toArray
  41. }

⑥ 计算每个Key所在Partition:当分区范围长度在128以内,使用顺序搜索来确定Key所在的Partition,否则使用二分查找算法来确定Key所在的Partition。

  1. /**
  2. * 获得每个Key所在的partitionId
  3. */
  4. def getPartition(key: Any): Int = {
  5. val k = key.asInstanceOf[K]
  6. var partition = 0
  7. // 如果得到的范围不大于128,则进行顺序搜索
  8. if (rangeBounds.length <= 128) {
  9. // If we have less than 128 partitions naive search
  10. while (partition < rangeBounds.length && ordering.gt(k, rangeBounds(partition))) {
  11. partition += 1
  12. }
  13. }
  14. // 范围大于128,则进行二分搜索该key所在范围,即可得到该key所在的partitionId
  15. else {
  16. // Determine which binary search method to use only once.
  17. partition = binarySearch(rangeBounds, k)
  18. // binarySearch either returns the match location or -[insertion point]-1
  19. if (partition < 0) {
  20. partition = -partition-1
  21. }
  22. if (partition > rangeBounds.length) {
  23. partition = rangeBounds.length
  24. }
  25. }
  26. if (ascending) {
  27. partition
  28. } else {
  29. rangeBounds.length - partition
  30. }
  31. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值