danielharan (owner)

Revisions

gist: 148102 Download_button fork
public
Description:
Refactoring an example from Hadoop: The Definitive Guide, 1st Edition
Public Clone URL: git://gist.github.com/148102.git
Embed All Files: show embed
functional_reducer.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env ruby
 
maxima = Hash.new(0)
 
STDIN.each_line do |line|
  key,val = line.split("\t")
  maxima[key] = [maxima[key], val.to_i].max
end
 
maxima.each do |key,val|
  puts [key,val].join("\t")
end
 
reducer.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env ruby
 
last_key, max_val = nil, 0
STDIN.each_line do |line|
  key, val = line.split("\t")
  if last_key && last_key != key
    puts "#{last_key}\t#{max_val}"
    last_key, max_val = key, val.to_i
  else
    last_key, max_val = key, [max_val, val.to_i].max
  end
end
puts "#{last_key}\t#{max_val}" if last_key