Created
July 12, 2010 14:44
-
-
Save nfo/472547 to your computer and use it in GitHub Desktop.
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
# The Riak object "b1/a" is linked to objects in "b2". | |
# The objects in "b2" contain a JSON hash with a "links" key. This list contains an array of ids. | |
# The objects "b3/idX" are the final objects, we can process them in maps and a reduce. | |
results = Riak::MapReduce.new(client). | |
add('b1', 'a'). | |
# link-walk to objects in 'b2' | |
link({:bucket => 'b2', :tag => '_', :keep => false}). | |
# return a array of tuples [bucket, id], that Riak will recognize and follow | |
map("function(v, keydata, arg) { return v.not_found ? [] : JSON.parse(v.values[0].data).links.map(function(x) { return [arg, x]; }); }", :keep => false, :arg => 'b3'). | |
# got a bunch of objects in 'b3', filter them | |
map("function(v) { item = JSON.parse(v.values[0].data); return item.to.match(/nicolas/) ? [item] : []; }", :keep => false). | |
# sort 'b3' items per date desc, and return the top 5 | |
reduce("function(values, arg) { sorted_values = values.sort(function(a, b) { if (a.date > b.date) { return -1; } else { return 1 } } ); return sorted_values.slice(0, arg) }", :keep => true, :arg => 5). | |
# default timeout is 60s | |
timeout(60). | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment