Class: MapReduce::HashPartitioner

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(num_partitions) ⇒ HashPartitioner

Initializes a HashPartitioner.

Examples:

MapReduce::HashPartitioner.new(16)

Parameters:

  • num_partitions (Fixnum)

    The desired number of partitions. Typically 8, 16, 32, 64, etc. but can be everything according to your needs.



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.

Examples:

partitioner.call("some key")

Parameters:

  • key

    The key to calculate the partition for. Can be everything that can be serialized as json.



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