当前位置:网站首页>12 Spark on RDD 分区器
12 Spark on RDD 分区器
2022-08-08 23:31:00 【YaPengLi.】
RDD 分区器
Spark 目前支持 Hash 分区和 Range 分区,和用户自定义分区。Hash 分区为当前的默认分区。分区器直接决定了 RDD 中分区的个数、RDD 中每条数据经过 Shuffle 后进入哪个分区,进而决定了 Reduce 的个数。只有 Key-Value 类型的 RDD 才有分区器,非 Key-Value 类型的 RDD 分区的值是 None每个 RDD 的分区 ID 范围:0 ~ (numPartitions - 1),决定这个值是属于那个分区的。
Hash 分区:对于给定的 key,计算其 hashCode,并除以分区个数取余。
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
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
}
Range 分区:将一定范围内的数据映射到一个分区中,尽量保证每个分区数据均匀,而且分区间有序
class RangePartitioner[K : Ordering : ClassTag, V](
partitions: Int,
rdd: RDD[_ <: Product2[K, V]],
private var ascending: Boolean = true)
extends Partitioner {
// We allow partitions = 0, which happens when sorting an empty RDD under the
default settings.
require(partitions >= 0, s"Number of partitions cannot be negative but found
$partitions.")
private var ordering = implicitly[Ordering[K]]
// An array of upper bounds for the first (partitions - 1) partitions
private var rangeBounds: Array[K] = {
...
}
def numPartitions: Int = rangeBounds.length + 1
private var binarySearch: ((Array[K], K) => Int) =
CollectionsUtils.makeBinarySearch[K]
def getPartition(key: Any): Int = {
val k = key.asInstanceOf[K]
var partition = 0
if (rangeBounds.length <= 128) {
// If we have less than 128 partitions naive search
while (partition < rangeBounds.length && ordering.gt(k,
rangeBounds(partition))) {
partition += 1
}
} else {
// Determine which binary search method to use only once.
partition = binarySearch(rangeBounds, k)
// binarySearch either returns the match location or -[insertion point]-1
if (partition < 0) {
partition = -partition-1 }
if (partition > rangeBounds.length) {
partition = rangeBounds.length
} }
if (ascending) {
partition
} else {
rangeBounds.length - partition
} }
override def equals(other: Any): Boolean = other match {
...
}
override def hashCode(): Int = {
...
}
@throws(classOf[IOException])
private def writeObject(out: ObjectOutputStream): Unit =
Utils.tryOrIOException {
...
}
@throws(classOf[IOException])
private def readObject(in: ObjectInputStream): Unit = Utils.tryOrIOException
{
...
} }
边栏推荐
猜你喜欢
(newcoder 15079)无关(容斥原理)
洛谷P4197 Peaks 线段树合并
Binary tree level traversal and examples
【CUDA】版本自由切换
(2022牛客多校五)G-KFC Crazy Thursday(二分+哈希/Manacher)
(2022牛客多校三)A-Ancestor(LCA)
【PP-YOLOv2】训练自定义的数据集
【latex异常与错误】There were undefined references.Reference `xxx‘ on page x undefined.参考引用公式编号时发生错误
51nod2614 小B爱旅行 (参考范艺杰代码 基本抄袭 太难了)
2022牛客多校六 A-Array(构造+哈夫曼)
随机推荐
[PP-YOLOv2] Training a custom dataset
线性筛求积性函数
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
(2022牛客多校五)H-Cutting Papers(签到)
[GYCTF2020]Ezsqli-1|SQL注入
mysql主从复制
ArrayAccess 接口用处
详解JS中for...of、in关键字
php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
Codeforces Round #738 (Div. 2) E
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
Manacher(求解最长回文子串)
-Wl,--start-group ... -Wl,--end-group 用于解决几个库的循环依赖关系
-Wl,--start-group ... -Wl,--end-group for resolving circular dependencies of several libraries
PHP 类函数和对象函数
(2022杭电多校六)1010-Planar graph(最小生成树)
(2022牛客多校五)D-Birds in the tree(树形DP)
(2022杭电多校五)1010-Bragging Dice (思维)
Low-Light Image Enhancement via a Deep Hybrid Network阅读札记
Virtual router redundancy protocol VRRP - double-machine hot backup