2011-02-27 22:15:52 -05:00
|
|
|
package spark
|
|
|
|
|
2012-10-12 21:19:21 -04:00
|
|
|
/**
|
|
|
|
* An object that defines how the elements in a key-value pair RDD are partitioned by key.
|
|
|
|
* Maps each key to a partition ID, from 0 to `numPartitions - 1`.
|
|
|
|
*/
|
2011-08-02 05:16:33 -04:00
|
|
|
abstract class Partitioner extends Serializable {
|
2011-02-27 22:15:52 -05:00
|
|
|
def numPartitions: Int
|
2011-03-07 02:38:16 -05:00
|
|
|
def getPartition(key: Any): Int
|
2011-02-27 22:15:52 -05:00
|
|
|
}
|
|
|
|
|
2012-10-12 21:19:21 -04:00
|
|
|
/**
|
|
|
|
* A [[spark.Partitioner]] that implements hash-based partitioning using Java's `Object.hashCode`.
|
|
|
|
*/
|
2011-03-07 02:38:16 -05:00
|
|
|
class HashPartitioner(partitions: Int) extends Partitioner {
|
2011-02-27 22:15:52 -05:00
|
|
|
def numPartitions = partitions
|
|
|
|
|
2012-07-12 21:36:02 -04:00
|
|
|
def getPartition(key: Any): Int = {
|
|
|
|
if (key == null) {
|
|
|
|
return 0
|
2012-02-10 11:19:53 -05:00
|
|
|
} else {
|
2012-07-12 21:36:02 -04:00
|
|
|
val mod = key.hashCode % partitions
|
|
|
|
if (mod < 0) {
|
|
|
|
mod + partitions
|
|
|
|
} else {
|
|
|
|
mod // Guard against negative hash codes
|
|
|
|
}
|
2012-02-10 11:19:53 -05:00
|
|
|
}
|
2011-02-27 22:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
2011-03-07 02:38:16 -05:00
|
|
|
case h: HashPartitioner =>
|
2011-02-27 22:15:52 -05:00
|
|
|
h.numPartitions == numPartitions
|
2012-02-10 11:19:53 -05:00
|
|
|
case _ =>
|
|
|
|
false
|
2011-02-27 22:15:52 -05:00
|
|
|
}
|
2012-02-11 03:56:28 -05:00
|
|
|
}
|
|
|
|
|
2012-10-12 21:19:21 -04:00
|
|
|
/**
|
|
|
|
* A [[spark.Partitioner]] that partitions sortable records by range into roughly equal ranges.
|
|
|
|
* Determines the ranges by sampling the RDD passed in.
|
|
|
|
*/
|
2012-03-17 16:08:36 -04:00
|
|
|
class RangePartitioner[K <% Ordered[K]: ClassManifest, V](
|
2012-06-09 17:44:18 -04:00
|
|
|
partitions: Int,
|
|
|
|
@transient rdd: RDD[(K,V)],
|
|
|
|
private val ascending: Boolean = true)
|
2012-02-11 03:56:28 -05:00
|
|
|
extends Partitioner {
|
|
|
|
|
2012-08-03 16:37:35 -04:00
|
|
|
// An array of upper bounds for the first (partitions - 1) partitions
|
2012-03-17 16:08:36 -04:00
|
|
|
private val rangeBounds: Array[K] = {
|
2012-08-03 16:37:35 -04:00
|
|
|
if (partitions == 1) {
|
2012-03-17 16:08:36 -04:00
|
|
|
Array()
|
|
|
|
} else {
|
2012-08-03 16:37:35 -04:00
|
|
|
val rddSize = rdd.count()
|
2012-09-26 03:25:34 -04:00
|
|
|
val maxSampleSize = partitions * 20.0
|
2012-08-03 16:37:35 -04:00
|
|
|
val frac = math.min(maxSampleSize / math.max(rddSize, 1), 1.0)
|
2012-09-26 03:25:34 -04:00
|
|
|
val rddSample = rdd.sample(false, frac, 1).map(_._1).collect().sortWith(_ < _)
|
2012-08-03 16:37:35 -04:00
|
|
|
if (rddSample.length == 0) {
|
|
|
|
Array()
|
|
|
|
} else {
|
|
|
|
val bounds = new Array[K](partitions - 1)
|
|
|
|
for (i <- 0 until partitions - 1) {
|
|
|
|
val index = (rddSample.length - 1) * (i + 1) / partitions
|
|
|
|
bounds(i) = rddSample(index)
|
|
|
|
}
|
|
|
|
bounds
|
2012-03-17 16:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 16:37:35 -04:00
|
|
|
def numPartitions = partitions
|
2012-02-11 03:56:28 -05:00
|
|
|
|
2012-03-17 16:08:36 -04:00
|
|
|
def getPartition(key: Any): Int = {
|
|
|
|
// TODO: Use a binary search here if number of partitions is large
|
2012-02-13 03:07:39 -05:00
|
|
|
val k = key.asInstanceOf[K]
|
2012-03-17 16:08:36 -04:00
|
|
|
var partition = 0
|
2012-08-03 16:37:35 -04:00
|
|
|
while (partition < rangeBounds.length && k > rangeBounds(partition)) {
|
2012-03-17 16:08:36 -04:00
|
|
|
partition += 1
|
|
|
|
}
|
|
|
|
if (ascending) {
|
|
|
|
partition
|
|
|
|
} else {
|
2012-08-03 16:37:35 -04:00
|
|
|
rangeBounds.length - partition
|
2012-03-17 16:08:36 -04:00
|
|
|
}
|
2012-02-11 03:56:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
2012-02-13 03:07:39 -05:00
|
|
|
case r: RangePartitioner[_,_] =>
|
2012-06-09 17:44:18 -04:00
|
|
|
r.rangeBounds.sameElements(rangeBounds) && r.ascending == ascending
|
2012-03-17 16:08:36 -04:00
|
|
|
case _ =>
|
|
|
|
false
|
2012-02-11 03:56:28 -05:00
|
|
|
}
|
|
|
|
}
|