views:

563

answers:

2

Does anyone have example code for mapreduce for Riak that can be run on a single Riak node.

+1  A: 
cd ~/riak
erl -name [email protected] -setcookie riak -pa apps/riak/ebin

In the shell:

# connect to the server
> {ok, Client} = riak:client_connect('[email protected]').
{ok,{riak_client,'[email protected]',<<6,201,208,64>>}}

# create and insert objects
> Client:put(riak_object:new(<<"groceries">>, <<"mine">>, ["eggs", "bacons"]), 1).
ok
> Client:put(riak_object:new(<<"groceries">>, <<"yours">>, ["eggs", "sausages"]), 1).
ok

# create Map and Reduce functions
> Count = fun(G, 'undefined', 'none') ->
            [dict:from_list([{I, 1} || I <- riak_object:get_value(G)])]
          end.
#Fun<erl_eval.18.105910772>
> Merge = fun(Gcounts, 'none') ->
            [lists:foldl(fun(G, Acc) ->
                           dict:merge(fun(_, X, Y) -> X+Y end, G, Acc)
                         end, dict:new(), Gcounts)] 
          end.
#Fun<erl_eval.12.113037538>

# do the map-reduce
> {ok, [R]} = Client:mapred([{<<"groceries">>, <<"mine">>},
                             {<<"groceries">>, <<"yours">>}],
                            [{'map', {'qfun', Count}, 'none', false},
                             {'reduce', {'qfun', Merge}, 'none', true}]).           
{ok,[{dict,...

> dict:to_list(R).
[{"eggs",2},{"susages",1},{"bacons",1}]

For the server I used absolutely default config:

$ hg clone http://hg.basho.com/riak/
$ cd riak
$ ./rebar compile generate
$ cd rel
$ ./riak/bin/riak start
Zed
Yes and I get several different errors depending on the configuration I use. eg:exception error: no match of right hand side value {error,{badarg,[{ets,match,[nodelocal_ring,{ring,'$1'}]}, {riak_ring_manager,get_my_ring,0}, {riak_mapreduce_fsm,init,1}, {gen_fsm,init_it,6}, {proc_lib,init_p_do_apply,3}]}}: is not very helpful
Zubair
What is needed is a step by step guide which actually works.
Zubair
Does the Map Reduce example from the manual work for you? If so maybe you could help me and talk me through getting it working?
Zubair
The documentation is a bit inconsistent, as in the client examples, bucket and object names are binaries, but in the map-reduce example they use atoms and strings.
Zed
Zed, you are a star! It works!!! Now, I'll try to get it working from modules, but that was a great help. :)
Zubair
Yes, you are right, I replaced the atoms for Strings and my previous code works now.
Zubair
+1  A: 

Here's an example of how to do a MapReduce using JavaScript functions.

Augustus