Class: MapReduce::HashPartitioner
- Inherits:
-
Object
- Object
- MapReduce::HashPartitioner
- Defined in:
- lib/map_reduce/hash_partitioner.rb
Overview
The MapReduce::HashPartitioner calculates a partition for the passed keys using SHA1 modulo the desired number of partitions.
Instance Method Summary collapse
-
#call(key) ⇒ Object
Calculates the partition for the specified key.
-
#initialize(num_partitions) ⇒ HashPartitioner
constructor
Initializes a HashPartitioner.
Constructor Details
#initialize(num_partitions) ⇒ HashPartitioner
Initializes a HashPartitioner.
15 16 17 |
# File 'lib/map_reduce/hash_partitioner.rb', line 15 def initialize(num_partitions) @num_partitions = num_partitions end |
Instance Method Details
#call(key) ⇒ Object
Calculates the partition for the specified key.
28 29 30 |
# File 'lib/map_reduce/hash_partitioner.rb', line 28 def call(key) Digest::SHA1.hexdigest(JSON.generate(key))[0..4].to_i(16) % @num_partitions end |