erlang - 使用 Riak 进行映射

标签 erlang mapreduce riak

有没有人有可以在单个 Riak 节点上运行的 Riak mapreduce 示例代码。

最佳答案

cd ~/riak
erl -name zed@127.0.0.1 -setcookie riak -pa apps/riak/ebin

在外壳中:

# connect to the server
> {ok, Client} = riak:client_connect('riak@127.0.0.1').
{ok,{riak_client,'riak@127.0.0.1',<<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}]

对于我使用绝对默认配置的服务器:

$ hg clone http://hg.basho.com/riak/
$ cd riak
$ ./rebar compile generate
$ cd rel
$ ./riak/bin/riak start

关于erlang - 使用 Riak 进行映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123004/

相关文章:

debugging - 使用 -compile 在 Erlang 模块中请求 debug_info

java - 无法访问 MapReduce 的 reducer 类中的计数器

transactions - 确保 riak 的正确性?

riak - 有没有办法为riak数据库提供身份验证?

erlang - ets :new for unnamed tables 中 name 参数的原因

erlang - Erlang 如何实现 erlang :display actually?

erlang - 使用lager log框架时总是出现lager_transform的undef错误

java - 在 Hive 中创建、添加和使用 UDF

hadoop - 在 Hadoop 中的多个文件中写入输出

mapreduce - 如何进行更快的 Riak MapReduce 查询?