简体   繁体   中英

Riak map reduce in erlang, error when try to list index/metadata

I am trying to use map reduce in Riak to do this:

  1. query over all keys in bucket X
  2. with index 'last_modify_int' between 1 and 10
  3. map the key and the value of the index tag_bin

in javascript the map will be

function(riakObject) {
          var indexes = riakObject.values[0].metadata.index;
          var tag_bin = indexes.tag_bin;

          return (tag_bin)? [
            [
              riakObject.key, tag_bin
            ]
          ] : [];
        }

but I can't do this in javascript. I am storing non-json data (binary) and I can't convert to JSON. and I can't convert to base64 or other formats.

I decide to use Erlang. but how I can fetch the index 'tag_bin' ?

I am trying to adapt this example just to list all index/metadata/etc without success.

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

I am storing data in bucket training, key baz, and indexes last_modify_int => 3, tag_bin => 'even'

but it throws one exception:

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.

If I try this function:

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

The error is:

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}]}]'

someone can help me?

EDIT:

DOING THIS:

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

I can return all indexes, but in some cases I have one index with more than one value and this way it returns only the last index value. Help.

Ok I discover the problem.

Index is an array of tuples and I was using keyfind to return the index value for tag_bin and it was returning the last element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM