使用erlang文件时riak中的错误函数子句

时间:2014-08-30 15:17:10

标签: python-2.7 erlang riak erlang-shell

这个

 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...\\"},...]},...]"}'

我在本地计算机上运行时没有收到错误,但是当我运行地图并减少服务器上的阶段时,我收到错误。

reduce阶段看起来像这样

-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].

当我使用地图阶段运行上述减少阶段时,我得到的数据在我的本地设置中如下所示

[{'xxxx@gmail.com': {'ab_leads': 2, 'cp_leads': 1},
'xxxx@gmail.com': {'ab_leads': 0, 'cp_leads': 1}}]

但是当我在服务器上执行相同的错误时,我收到错误,请帮助我

1 个答案:

答案 0 :(得分:2)

您收到function_clause错误,因为您传递给fold的fun将永远不会匹配列表L中的项目。我试着在下面解释和重构你的代码。

-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).