Created
April 19, 2011 15:05
-
-
Save dsingley/928288 to your computer and use it in GitHub Desktop.
Hadoop Streaming API for Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def emit(key, value, sep="\t") | |
STDOUT.puts('' << key << sep << value) | |
end | |
def map(*options) | |
options = [:split, "\t", 2] if options.empty? | |
STDIN.each_line do |line| | |
line.strip! | |
key, value = line.send(*options) | |
yield key, value | |
end | |
end | |
def reduce(*options) | |
options = [:split, "\t", 2] if options.empty? | |
current_key = nil | |
current_values = [] | |
STDIN.each_line do |line| | |
line.strip! | |
key, value = line.send(*options) | |
if current_key != key && current_key != nil | |
yield current_key, current_values | |
current_values.clear | |
end | |
current_key = key | |
current_values << value | |
end | |
yield current_key, current_values unless current_key.nil? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment