HashPartitioner

HashPartitioner is a Partitioner for hash-based partitioning.

HashPartitioner is used as the default Partitioner.

Number of Partitions

HashPartitioner takes a number of partitions to be created.

HashPartitioner uses the number of partitions to find the partition ID for a given key (of a key-value record).

Finding Partition ID for Key

getPartition(key: Any): Int

getPartition returns 0 as the partition ID for null keys.

For non-null keys, getPartition uses the key’s Object.hashCode modulo the configured number of partitions. For a negative result, getPartition adds the number of partitions (used for the modulo operator) to make it positive.

getPartition is part of the Partitioner abstraction.

equals Method

equals(other: Any): Boolean

Two HashPartitioners are considered equal when the number of partitions are the same.

hashCode Method

hashCode: Int

hashCode is the number of partitions.