----------这是客户端测试代码-------------------------
-module(test).
-export([test/0,
get_state/0]).
get_state() ->
Bucket = <<"ejabberd/pubsub_state">> ,
{ok, Pid}= riakc_pb_socket:start("127.0.0.1", 8087),
Inputs = {index, Bucket, <<"idx_int">>, 1}, %% that is ok
%Query = [],
Query =
[{map, {modfun, trend_riak, get_map_bucket_values}, none, true}], %%注意这里的trend_riak:get_map_bucet_values是自己在riak-server中自定义的
%[{map, {modfun, trend_riak, get_map_bucket_values}, none, false},
% {reduce, {modfun, trend_riak, get_reduce_bucket_values}, {1,2}, true}],
riakc_pb_socket:mapred(Pid, Inputs, Query).
test() ->
Bucket = <<"ejabberd/pubsub_node">> ,
{ok, Pid}= riakc_pb_socket:start("127.0.0.1", 8087),
%Inputs = {modfun, trend_riak, test, [<<"a">>, <<"b">>]},
%Inputs = {modfun, riak_search, mapred_search, [ <<"Bucket">>, <<"SearchQuery">>]}, %% that is ok
%Inputs = {index, Bucket, <<"nodeidx_int">>,11}, %% that is ok
Inputs = {index, Bucket, <<"host_bin">>,<<"pubsub.meda.com">>}, %% that is ok
%Query = [],
Query =
[{map, {modfun, trend_riak, test}, none, true}],
%[{map, {modfun, trend_riak, get_map_bucket_values}, none, false},
% {reduce, {modfun, trend_riak, get_reduce_bucket_values}, {1,2}, true}],
riakc_pb_socket:mapred(Pid, Inputs, Query).
test1() ->
Bucket = <<"ejabberd/pubsub_item">> ,
%Merge = fun(Gcounts, none) -> lists:keysort(6,Gcounts) end,
%Count = fun(G, undefined, none) -> [binary_to_term(riak_object:get_value(G))] end,
%Query = [{map, {qfun, Count}, none,false}, {reduce, {qfun, Merge}, none, true}],
{ok, Pid}= riakc_pb_socket:start("127.0.0.1", 8087),
%riakc_pb_socket:mapred(Pid, Bucket, [{map, {modfun, riak_kv_mapreduce, map_object_value}, none,true}]).
%riakc_pb_socket:mapred(Pid, Bucket, [{map, {modfun, riak_kv_mapreduce, map_object_value}, none,true}]).
%Query = [{map, {modfun, riak_kv_mapreduce, map_object_value},
% {struct,[{<<"sub">>,[<<"0">>]}]},false},
% {reduce,{modfun, riak_kv_mapreduce, reduce_string_to_integer},none,false},
% {reduce,{modfun, riak_kv_mapreduce, reduce_sum}, none,true}],
%Query =
% [{map, {modfun, riak_kv_mapreduce, map_object_value}, none,true},
% {reduce, {modfun, riak_kv_mapreduce, reduce_sum}, none, true}],
Query =
[{map, {modfun, trend_riak, get_map_bucket_values}, none, false},
% [{map, {modfun, riak_kv_mapreduce, map_object_value}, none,true},
{reduce, {modfun, trend_riak, get_reduce_bucket_values}, {1,2}, true}],
riakc_pb_socket:mapred(Pid, Bucket, Query).
----------------------自定义服务器代码------------------------
1 -module(trend_riak).
2
3 -export([get_map_bucket_values/3,
4 get_reduce_bucket_values/2,
5 test/3,
6 test/2]).
7
8 -record(pubsub_item,
9 {
10 itemid,
11 creation = {'unknown','unknown'},
12 modification = {'unknown','unknown'},
13 payload = [],
14 published = "",
15 node_icon = "",
16 node_name = "",
17 node_creator = "",
18 node_creator_jid = "",
19 node_creator_icon = "",
20 creator = "",
21 creator_jid = "",
22 creator_icon = "",
23 category = ""
24 }).
25
26 get_map_bucket_values(Record, undefined, {Nodes, CategoryList})->
27 PubsubItem = binary_to_term(riak_object:get_value(Record)),
28 IsNode =lists:member(PubsubItem#pubsub_item.node_name, Nodes),
29 IsCategory = lists:member(PubsubItem#pubsub_item.category, CategoryList),
30 if IsNode == true orelse
31 IsCategory == true ->
32 [PubsubItem];
33 true ->
34 []
35 end;
36 %% _s default is undefined _c default is none
37 get_map_bucket_values(Record, undefined, _C)->
38 [binary_to_term(riak_object:get_value(Record))].
39
40 get_reduce_bucket_values(Records,{Start, Max})->
41 %Records.
42 %%lists:keysort(6,Records).
43 %lists:sublist(Records, Start, Max).
44 Skip = get_skip_num(Start, Max),
45 if length(Records) > Skip ->
46 lists:reverse(lists:sublist(lists:keysort(#pubsub_item.published, Records), Skip, Max));
47 true ->
48 []
49 end.
50 test(A,B) ->
51 ok.
52 test(Record, B, C) -> %%B,C两个参数可以有客户端传入
53 [binary_to_term(riak_object:get_value(Record))].
%[A].
55 %% io:format(ssssssssss),
56 %% io:format("A=~p,B=~p,C=~p~n", [A,B,C]),
57 %{ok, [<<"itrends/ejabberd/pubsub_node">>]}.
58 %% [{<<"my">>, <<"bu">>}].
59 %{ok,[]}.
60 %[].
61
62
63
64 get_skip_num(1, _Max) ->
65 1;
66 get_skip_num(Start, Max) ->
67 Start*Max.
这里需要注意的是需要在riak的
vi etc/vm.args中添加如下:
40 ## add by langxw
41 -pa /home/jason/learn/riak
主要目的是在riak中引入trend_riak.erl
~