Write your first MapReduce program in 20 minutes
michaelnielsen.org
Write your first MapReduce program in 20 minutes
1–10 of 16 posts
Re: Write your first MapReduce program in 20 minutes
#2 result = Dir.glob('test?.txt').collect do |file_name|
File.new(file_name, 'r').read.split(' ').collect do |word|
word.downcase.tr '.,\'', ''
end.inject Hash.new(0) do |hash,word|
hash[word] += 1
hash
end
end.inject do |all,hash|
(all.keys + hash.keys).uniq.inject Hash.new(0) do |acc,word|
acc[word] = all[word] + hash[word]
acc
end
end
p result
Edit: The Python example in the article is better because it merges hashes in the reduce step which facilitates parallelisation.Re: Write your first MapReduce program in 20 minutes
#3http://engineeringblog.yelp.com/2010/10/mrjob-distributed-co...
Re: Write your first MapReduce program in 20 minutes
#4Re: Write your first MapReduce program in 20 minutes
#5And here's the equivalent Ruby program. In Ruby it is usually 'collect' and 'inject' instead of 'map' and 'reduce'. result = Dir.glob('test?.txt').collect do |file_name| File.new(file_name, 'r').read.split(' ').collect do |word| word.downcase.tr '.,\'', '' end.inject Hash.new(0) do |hash,word| hash[word] += 1 hash end end.inject do |all,hash| (all.keys + hash.keys).uniq.inject Hash.new(0) do |acc,word| acc[word] = al…
Re: Write your first MapReduce program in 20 minutes
#6If you really want to try and play with MapReduce on a dataset without going through setting up a Hadoop node (or 4), check out CouchDB. It's designed around MapReduces (though not distributed), and you even get to deal with solving re-reduce problems.
Re: Write your first MapReduce program in 20 minutes
#7And here's the equivalent Ruby program. In Ruby it is usually 'collect' and 'inject' instead of 'map' and 'reduce'. result = Dir.glob('test?.txt').collect do |file_name| File.new(file_name, 'r').read.split(' ').collect do |word| word.downcase.tr '.,\'', '' end.inject Hash.new(0) do |hash,word| hash[word] += 1 hash end end.inject do |all,hash| (all.keys + hash.keys).uniq.inject Hash.new(0) do |acc,word| acc[word] = al…
You can also use Enumerable#map and Enumerable#reduce, to use names that match the pattern (and #map is arguably more idiomatic than #collect).
Re: Write your first MapReduce program in 20 minutes
#8Re: Write your first MapReduce program in 20 minutes
#9And here's the equivalent Ruby program. In Ruby it is usually 'collect' and 'inject' instead of 'map' and 'reduce'. result = Dir.glob('test?.txt').collect do |file_name| File.new(file_name, 'r').read.split(' ').collect do |word| word.downcase.tr '.,\'', '' end.inject Hash.new(0) do |hash,word| hash[word] += 1 hash end end.inject do |all,hash| (all.keys + hash.keys).uniq.inject Hash.new(0) do |acc,word| acc[word] = al…
Re: Write your first MapReduce program in 20 minutes
#10Earlier quoted context omitted.
You can also use Enumerable#map and Enumerable#reduce, to use names that match the pattern (and #map is arguably more idiomatic than #collect).
I finally got inject() when they added the reduce() alias.