Created
July 7, 2011 15:38
-
-
Save flazz/1069785 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rubygems' | |
require 'riak' | |
require 'json' | |
require 'benchmark' | |
require 'pp' | |
$client = Riak::Client.new :protocol => :pbc | |
def latest_10_via_values | |
mr = Riak::MapReduce.new($client). | |
add("events"). | |
map(<<-MAP). | |
function(v) { | |
var data = Riak.mapValuesJson(v)[0] | |
var epoch_ms = parseInt(data.created_at, 16); | |
return [ { time : epoch_ms, key : v.key } ]; | |
} | |
MAP | |
reduce(<<-REDUCE, :keep => true) | |
function(vs) { | |
var vs_sorted = vs.sort(function(a,b){ return b.time - a.time; }); | |
return vs_sorted.slice(0,10); | |
} | |
REDUCE | |
results = mr.run | |
puts results | |
end | |
puts Benchmark.measure { latest_10_via_values } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment