Skip to content

HashPartitioner

HashPartitioner is a Partitioner for hash-based partitioning.

Important

HashPartitioner places null keys in 0th partition.

HashPartitioner is used as the default Partitioner.

Creating Instance

HashPartitioner takes the following to be created:

  • Number of partitions

Number of Partitions

numPartitions: Int

numPartitions returns the given number of partitions.

numPartitions is part of the Partitioner abstraction.

Partition for Key

getPartition(
  key: Any): Int

For null keys getPartition simply returns 0.

For non-null keys, getPartition uses the Object.hashCode of the key modulo the number of partitions. For negative results, getPartition adds the number of partitions to make it non-negative.

getPartition is part of the Partitioner abstraction.