0fba22b3d2
Note that we use scala.Serializable introduced in Scala 2.9 instead of java.io.Serializable. Also, case classes inherit from scala.Serializable by default.
21 lines
536 B
Scala
21 lines
536 B
Scala
package spark
|
|
|
|
abstract class Partitioner extends Serializable {
|
|
def numPartitions: Int
|
|
def getPartition(key: Any): Int
|
|
}
|
|
|
|
class HashPartitioner(partitions: Int) extends Partitioner {
|
|
def numPartitions = partitions
|
|
|
|
def getPartition(key: Any) = {
|
|
val mod = key.hashCode % partitions
|
|
if (mod < 0) mod + partitions else mod // Guard against negative hash codes
|
|
}
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
|
case h: HashPartitioner =>
|
|
h.numPartitions == numPartitions
|
|
case _ => false
|
|
}
|
|
} |