Skip to content

Instantly share code, notes, and snippets.

@flazz
Created July 7, 2011 15:38
Show Gist options
  • Save flazz/1069785 to your computer and use it in GitHub Desktop.
Save flazz/1069785 to your computer and use it in GitHub Desktop.
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