Class: Kafka::Partitioner
- Inherits:
-
Object
- Object
- Kafka::Partitioner
- Defined in:
- lib/kafka/partitioner.rb
Overview
Assigns partitions to messages.
Instance Method Summary collapse
-
#initialize(partitions) ⇒ Partitioner
constructor
A new instance of Partitioner.
-
#partition_for_key(key) ⇒ Integer
Assigns a partition number based on a key.
Constructor Details
#initialize(partitions) ⇒ Partitioner
7 8 9 |
# File 'lib/kafka/partitioner.rb', line 7 def initialize(partitions) @partitions = partitions end |
Instance Method Details
#partition_for_key(key) ⇒ Integer
Assigns a partition number based on a key.
If the key is nil, then a random partition is selected. Otherwise, a digest of the key is used to deterministically find a partition. As long as the number of partitions doesn't change, the same key will always be assigned to the same partition.
20 21 22 23 24 25 26 |
# File 'lib/kafka/partitioner.rb', line 20 def partition_for_key(key) if key.nil? rand(@partitions.count) else Zlib.crc32(key) % @partitions.count end end |