基于Riak数据库的Map/Reduce实现

参考资料:

http://wiki.basho.com/MapReduce.html#MapReduce-via-the-Erlang-API

https://github.com/basho/riak-erlang-client/blob/master/docs/pb-client.txt

注意:以下代码尚未完全调试通过,仅供参考。

$ erl -pa $PATH_TO_RIAKC/ebin $PATH_TO_RIAKC/deps/*/ebin

{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087).
Mine = riakc_obj:new(<<"groceries">>, <<"mine">>, ["eggs", "bacon"]).
Yours = riakc_obj:new(<<"groceries">>, <<"yours">>, ["bread", "bacon"]).
riakc_pb_socket:put(Pid, Mine).
riakc_pb_socket:put(Pid, Yours).
Count = fun(G, undefined, none) -> [dict:from_list([{I, 1} || I <- riakc_obj:get_value(G)])] end.
Merge = fun(Gcounts, none) -> [lists:foldl(fun(G, Acc) -> dict:merge(fun(_, X, Y) -> X+Y end, G, Acc) end, dict:new(), Gcounts)] end.
riakc_pb_socket:mapred(Pid,[{<<"groceries">>, <<"mine">>}, {<<"groceries">>, <<"yours">>}], [{map, {qfun, Count}, none, false}, {reduce, {qfun, Merge}, none, true}]).


你可能感兴趣的:(数据库,list,socket,Path,merge,fun)