【问题标题】:Error fucntion clause in riak while using erlang file使用erlang文件时有风险的错误函数子句
【发布时间】:2014-10-24 09:14:18
【问题描述】:

为此

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

当我在我的本地机器上运行时我没有收到错误,但是当我在服务器上运行 map 和 reduce 阶段时我收到了错误。

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

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

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

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

【问题讨论】:

    标签: python-2.7 erlang riak erlang-shell


    【解决方案1】:

    您收到 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).
    

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 1970-01-01
      • 2021-04-03
      • 2015-01-14
      • 1970-01-01
      • 2015-03-20
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多