riak map在erlang中减少,尝试列出索引/元数据时出错

时间:2013-12-11 16:44:36

标签: mapreduce erlang riak

我正在尝试使用Riak中的map reduce来执行此操作:

  1. 查询存储桶X中的所有密钥
  2. 索引'last_modify_int'介于1和10之间
  3. 映射关键字和索引tag_bin
  4. 的值

    在javascript中地图将是

    function(riakObject) {
              var indexes = riakObject.values[0].metadata.index;
              var tag_bin = indexes.tag_bin;
    
              return (tag_bin)? [
                [
                  riakObject.key, tag_bin
                ]
              ] : [];
            }
    

    但我不能在javascript中这样做。我存储非json数据(二进制),我无法转换为JSON。我无法转换为base64或其他格式。

    我决定使用Erlang。但我如何获取索引'tag_bin'?

    我正在尝试调整此示例只是为了列出所有索引/元数据/等而没有成功。

    -module(mr_example).
    
    -export([get_keys/3]).
    
    % Returns bucket and key pairs from a map phase
    get_keys(Value,_Keydata,_Arg) ->
      [{riak_object:bucket(Value), riak_object:index_data(Value) }].
    

    我将数据存储在桶训练,关键baz和索引中last_modify_int => 3,tag_bin => '偶数'

    但它引发了一个例外:

    Error in 'map_reduce' : Riak Error (code: 0) '{"phase":0,"error":"undef","input":"{ok,{r_object,<<\"training\">>,<<\"baz\">>,[{r_content,{dict,4,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[[<<\"content-type\">>,112,108,97,105,110,47,116,101,120,116],[<<\"X-Riak-VTag\">>,53,100,98,54,69,79,103,103,70,75,70,48,85,105,50,110,73,78,57,101,101,69]],[[<<\"index\">>,{<<\"last_modify_int\">>,3},{<<\"tag_bin\">>,<<\"even\">>}]],[],[[<<\"X-Riak-Last-Modified\">>|{1386,780017,102696}]],[],[]}}},<<86,48,45,61,115,114,108,1,0,40,43,1,40,43,8,32,206,...>>}],...},...}","type":"error","stack":"[{riak_object,index_data,[{r_object,<<\"training\">>,<<\"baz\">>,[{r_content,{dict,4,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[[<<\"content-type\">>,112,108,97,105,110,47,116,101,120,116],[<<\"X-Riak-VTag\">>,53,100,98,54,69,79,103,103,70,75,70,48,85,105,50,110,73,78,57,101,101,69]],[[<<\"index\">>,{<<\"last_modify_int\">>,3},{<<\"tag_bin\">>,<<\"even\">>}]],[],[[<<\"X-Riak-Last-Modified\">>|{1386,780017,102696}]],[],[]}}},<<86,48,45,61,115,114,108,1,...>>}],...}],...},...]"}' at t/17_2i_map_reduce.t line 72.
    

    如果我尝试这个功能:

    get_keys(Value,_Keydata,_Arg) ->
      [{riak_object:bucket(Value), riak_object:get_metadata(Value) }].
    

    错误是:

    Error in 'map_reduce' : Riak Error (code: 0) 'Error processing stream message: exit:{json_encode,
                                           {bad_term,
                                            {dict,4,16,16,8,80,48,
                                             {[],[],[],[],[],[],[],[],[],[],[],[],
                                              [],[],[],[]},
                                             {{[],[],[],[],[],[],[],[],[],[],
                                               [[<<"content-type">>,112,108,97,
                                                 105,110,47,116,101,120,116],
                                                [<<"X-Riak-VTag">>,50,103,117,98,
                                                 121,107,119,109,102,117,120,73,
                                                 100,108,74,101,86,108,122,75,55,
                                                 70]],
                                               [[<<"index">>,
                                                 {<<"last_modify_int">>,3},
                                                 {<<"tag_bin">>,<<"even">>}]],
                                               [],
                                               [[<<"X-Riak-Last-Modified">>|
                                                 {1386,780159,925964}]],
                                               [],[]}}}}}:[{mochijson2,
                                                            json_encode,2,
                                                            [{file,
                                                              "src/mochijson2.erl"},
                                                             {line,149}]},
                                                           {mochijson2,
                                                            '-json_encode_proplist/2-fun-0-',
                                                            3,
                                                            [{file,
                                                              "src/mochijson2.erl"},
                                                             {line,167}]},
                                                           {lists,foldl,3,
                                                            [{file,"lists.erl"},
                                                             {line,1197}]},
                                                           {mochijson2,
                                                            json_encode_proplist,
                                                            2,
                                                            [{file,
                                                              "src/mochijson2.erl"},
                                                             {line,170}]},
                                                           {riak_kv_pb_mapred,
                                                            msgs_for_results,4,
                                                            [{file,
                                                              "src/riak_kv_pb_mapred.erl"},
                                                             {line,205}]},
                                                           {riak_kv_pb_mapred,
                                                            process_stream,3,
                                                            [{file,
                                                              "src/riak_kv_pb_mapred.erl"},
                                                             {line,89}]},
                                                           {riak_api_pb_server,
                                                            process_stream,5,
                                                            [{file,
                                                              "src/riak_api_pb_server.erl"},
                                                             {line,246}]},
                                                           {riak_api_pb_server,
                                                            handle_info,2,
                                                            [{file,
                                                              "src/riak_api_pb_server.erl"},
                                                             {line,129}]}]'
    
    有人可以帮帮我吗?

    编辑:

    这样做:

    -module(mr_example).
    
    -export([get_keys/3]).
    
    % Returns bucket and key pairs from a map phase
    get_keys(Value,_Keydata,_Arg) ->
      Meta  = riak_object:get_metadata(Value),
      Index = dict:fetch(<<"index">>, Meta),
      [{riak_object:key(Value), Index} ].
    

    我可以返回所有索引,但在某些情况下,我有一个索引具有多个值,这样它只返回最后一个索引值。帮助

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。

索引是一个元组数组,我使用keyfind返回tag_bin的索引值,并返回最后一个元素。