Created
July 12, 2010 15:01
-
-
Save nfo/472565 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
# Following http://gist.github.com/472547 | |
# If links are already in the 'b2' objects, then getting the top 5 can be done in the first map function | |
results = Riak::MapReduce.new(client). | |
add('b1', 'a'). | |
link({:bucket => 'b2', :tag => '_', :keep => false}). | |
# This first map only emits 5 items instead of all of them | |
map("function(v, keydata, arg) { return v.not_found ? [] : JSON.parse(v.values[0].data).links.map(function(x) { return [arg, x]; }).slice(0, 5); }", :keep => false, :arg => 'b3'). | |
# The following map did not get the 5 items in the correct order ... | |
map("function(v) { item = JSON.parse(v.values[0].data); return [item]; }", :keep => false). | |
# ... so a reduce is still necessary (but hey, it's just 5 items) | |
reduce("function(values, arg) { return values.sort(function(a, b) { if (a.date > b.date) { return -1; } else { return 1 } } ); }", :keep => true). | |
timeout(120). | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment