Reputation: 155
for this
Error running MapReduce operation. Headers: {'content-length': '688', 'server': 'MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)', 'connection': 'close', 'date': 'Sat, 30 Aug 2014 14:39:59 GMT', 'content-type': 'application/json', 'http_code': 500} Body: '{"phase":1,"error":"function_clause","input":"[{<<\\"5ItqEz0wfkVWSp9YbFhw5vwETFL\\">>,[{<<\\"ab_leads\\">>,1},{<<\\"cp_leads\\">>,0}]}]","type":"error","stack":"[{report_reduce_totalchatcount,\'-reduce_totalchatcount/2-fun-0-\',[[{<<\\"5ItqEz0wfkVWSp9YbFhw5vwETFL\\">>,[{<<\\"ab_leads\\">>,17},{<<\\"cp_leads\\">>,1}]},{<<\\"YuqlGCpn1MS5eMUN13RJkWSqHjj\\">>,[{<<\\"ab_leads\\">>,2},{<<\\"cp_leads\\">>,0}]}],[]],[{file,\\"report_reduce_totalchatcount.erl\\"},{line,17}]},{lists,foldl,3,[{file,\\"lists.erl\\"},{line,1197}]},{report_reduce_totalchatcount,reduce_totalchatcount,2,[{file,\\"report_reduce_totalchatcount.erl\\"},{line,17}]},{riak_kv_w_reduce,reduce,3,[{file,\\"src/riak_kv_w_reduce.e...\\"},...]},...]"}'
i am not getting the error when i run in my local machine, but when i run the map and reduce phase on the serveri am getting the error.
the reduce phase looks like this
-export([reduce_totalchatcount/2]).
reduce_totalchatcount(L,_Arg) ->
Value = lists:foldl(fun([{Key,[{C,Ab_leads},{A,Cp_leads}]}], Acc) ->
[{C,Ab_leadsAcc},{A,Cp_leadsAcc}] = proplists:get_value(Key, Acc, [{C,0},{A,0}]),
[{Key,[{C,Ab_leadsAcc + Ab_leads},{A,Cp_leadsAcc + Cp_leads}]} | proplists:delete(Key, Acc)]
end,
[],
L),
[Value].
The data i am getting when i run the above reduce phase with map phase is in my local setup is like below
[{'[email protected]': {'ab_leads': 2, 'cp_leads': 1},
'[email protected]': {'ab_leads': 0, 'cp_leads': 1}}]
But i am getting the error when i execute the same error on server,please help me
Upvotes: 1
Views: 64
Reputation: 1495
You're getting a function_clause
error because the fun
you passed to fold will never match an item in the list L
. I've tried to interpret and refactor your code below.
-export([reduce_totalchatcount/2]).
fold_total_chatcount({Key, Props}, Acc) ->
%% I'm not assuming here that the keys in the Props are in any
%% particular order. The main action of the fold is to find any
%% existing key that matches and merge its props with this one.
case lists:keytake(Key, 1, Acc) of
{value, {_, Props1}, Acc1} ->
%% Merge props with the found key
[{Key, merge(Props, Props1)}|Acc1];
false ->
%% It doesn't exist so we can just add it to the Acc
[{Key, Props}|Acc]
end.
merge(Props1, Props2) ->
%% See http://www.erlang.org/doc/man/orddict.html#from_list-1 and
%% http://www.erlang.org/doc/man/orddict.html#merge-3
orddict:merge(fun(_, V1, V2) -> V1 + V2 end,
orddict:from_list(Props1),
orddict:from_list(Props2)).
reduce_total_chatcount(L, _Arg) ->
%% Since lists:foldl/3 is returning a list here, we don't
%% need to return it wrapped in a list.
lists:foldl(fun fold_total_chatcount/2, [], L).
Upvotes: 2