Skip to content

Instantly share code, notes, and snippets.

@kuenishi
Last active December 26, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuenishi/7226938 to your computer and use it in GitHub Desktop.
Save kuenishi/7226938 to your computer and use it in GitHub Desktop.
Multi-get emulation by listing keys
require 'riak'
host = 'localhost'
port = 8087
client = Riak::Client.new(:protocol => "pbc",
:nodes => [{:host => host, :http_port => port}])
r = Riak::MapReduce.new(client)
.add('mybucket', 'spam') # data is foobar
.add('mybucket', 'ham') # data is foobar2
.map(:function => [:riak_kv_mapreduce, :map_object_value],
:language => 'erlang', :arg => nil, :keep => true)
.run
p r
b = Riak::Bucket.new(client, 'mybucket')
r = Riak::Multiget.get_all(client, [[b, 'spam'], [b, 'ham']])
p r[[b, 'spam']].content.raw_data.to_s
p r[[b, 'ham']].content.raw_data.to_s
@kuenishi
Copy link
Author

$ bundle exec ruby multiget.rb
["foobar2", "foobar"]
"foobar"
"foobar2"

@benjaminbarbe
Copy link

Thanks @kuenishi

If you want to ignore not found objects:

r = Riak::MapReduce.new(client)
  .add('mybucket', 'spam') # data is foobar
  .add('mybucket', 'ham')  # data is foobar2
  .map(:function => [:riak_kv_mapreduce, :map_object_value],
       :language => 'erlang', :arg => 'filter_notfound', :keep => true)
  .run

It can be useful for other 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment