Skip to content

Instantly share code, notes, and snippets.

@randysecrist
Created February 13, 2012 20:33
Show Gist options
  • Save randysecrist/1819981 to your computer and use it in GitHub Desktop.
Save randysecrist/1819981 to your computer and use it in GitHub Desktop.
Riak wiki map reduce example that counts words in plain text files, using the ruby client.
require 'riak'
client = Riak::Client.new(:protocol => 'pbc')
map_phase = <<-EOF
function(v) {
var m = v.values[0].data.toLowerCase().match(/\\w*/g);
var r = [];
for(var i in m) {
if(m[i] != '') {
var o = {};
o[m[i]] = 1;
r.push(o);
}
}
return r;
}
EOF
reduce_phase = <<-EOF
function(v) {
var r = {};
for(var i in v) {
for(var w in v[i]) {
if(w in r) r[w] += v[i][w];
else r[w] = v[i][w];
}
}
return [r];
}
EOF
results = Riak::MapReduce.new(client).
add("alice", "p1").
add("alice", "p2").
add("alice", "p5").
map(map_phase).reduce(reduce_phase, :keep => true).run
puts results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment