0

I am working on riak-erlang client. while doing Mapreduce i got the following output.

Now i want to get the data from the Result set that i got.I want age/name, and also by particular name i want to get age.

How can i do that.please help me

{ok,[{0,R}]} = riakc_pb_socket:mapred(Pid,<<"test">>,[{map,{qfun,Mapf},none,true}]).
{ok,[{0,
  [<<"{\"age\": 24, \"name\": \"krishna\"}">>,
   <<"{\"age\": 29, \"name\": \"sharat\"}">>,
   <<"{\"age\": 25, \"name\": \"ramesh\"}">>,
   <<"{\"age\": 28, \"name\": \"kumar\"}">>,
   <<"{\"age\": 24, \"name\": \"gopi\"}">>,
   <<"{\"age\": 27, \"name\": \"anil\"}">>]}]}

LIKE: age: 24

or

name: "krishna"

or if i give name:krishna

age:24

How can i get the data

Krish gopi
  • 155
  • 4
  • 12

2 Answers2

1

Basically, you get json encoded data, so first you need to decode it and then implement some filtering/finding mechanism. My approach would be to use jiffy json parser:

First, clone and build jiffy:

git clone git@github.com:davisp/jiffy.git;
cd jiffy; make

You have to add jiffy to code path while running erlang command line client:

erl -pa Private/jiffy/ebin -pa Private/jiffy/deps

And finally, implementation for getting age based on name:

-module(test).
-compile(export_all).

decode(Results) ->
   [jiffy:decode(E)||E<-Results].

get_age(_, []) ->
    erlang:throw(name_not_found);
get_age(Name, [{H}|T]) ->
    case proplists:get_value(<<"name">>, H) of
        Name -> proplists:get_value(<<"age">>, H);
        _ -> get_age(Name, T)
    end.

Usage:

erl -pa Private/jiffy/ebin -pa Private/jiffy/deps
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.4  (abort with ^G)
1> application:start(jiffy).
ok
2> c(test).
{ok,test}
3> {ok, [{0, Results}]} = {ok,[{0,
3>   [<<"{\"age\": 24, \"name\": \"krishna\"}">>,
3>    <<"{\"age\": 29, \"name\": \"sharat\"}">>,
3>    <<"{\"age\": 25, \"name\": \"ramesh\"}">>,
3>    <<"{\"age\": 28, \"name\": \"kumar\"}">>,
3>    <<"{\"age\": 24, \"name\": \"gopi\"}">>,
3>    <<"{\"age\": 27, \"name\": \"anil\"}">>]}]}.
{ok,[{0,
      [<<"{\"age\": 24, \"name\": \"krishna\"}">>,
       <<"{\"age\": 29, \"name\": \"sharat\"}">>,
       <<"{\"age\": 25, \"name\": \"ramesh\"}">>,
       <<"{\"age\": 28, \"name\": \"kumar\"}">>,
       <<"{\"age\": 24, \"name\": \"gopi\"}">>,
       <<"{\"age\": 27, \"name\": \"anil\"}">>]}]}
4> Decoded = test:decode(Results).
[{[{<<"age">>,24},{<<"name">>,<<"krishna">>}]},
 {[{<<"age">>,29},{<<"name">>,<<"sharat">>}]},
 {[{<<"age">>,25},{<<"name">>,<<"ramesh">>}]},
 {[{<<"age">>,28},{<<"name">>,<<"kumar">>}]},
 {[{<<"age">>,24},{<<"name">>,<<"gopi">>}]},
 {[{<<"age">>,27},{<<"name">>,<<"anil">>}]}]
5> test:get_age(<<"krishna">>, Decoded).
24
6>
mkorszun
  • 4,461
  • 6
  • 28
  • 43
  • what you have said is right,but i am already using the erlangshell with riak-erlang client by erl -pa /home/downloads/riak-erlang-client/ebin/ . Then if use jiffy to start erlang shell then i wont get the prperties of the riak-erlang client properties . – Krish gopi May 19 '14 at 11:45
  • i am not sure if i get your point, but you can add multiple directories to code path using erl client. You could just call 'erl -pa JIFFY_PATHS -pa RIAK_CLIENT' – mkorszun May 19 '14 at 12:08
  • oh..i don't know about that. if its happens then it is good.is there any other option without Jiffy @Mkorszun – Krish gopi May 19 '14 at 12:20
  • If we use riak server and i want to sent the mapreduce written in erlang from the python proggrame. then i have to install jiffy in the server where riak is running. – Krish gopi May 19 '14 at 12:26
  • If you need some alternatives for jiffy, check this http://stackoverflow.com/questions/2395992/what-is-the-most-mature-json-library-for-erlang. – mkorszun May 19 '14 at 12:38
  • i am getting this error: Initialized empty Git repository in /tmp/jiffy/.git/ The authenticity of host 'github.com (192.30.252.131)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts. Permission denied (publickey). – Krish gopi May 19 '14 at 14:25
  • mochijson2 is inbuilt in riak or what? – Krish gopi May 21 '14 at 13:03
1

An alternate approach if you are not locked in to using JSON: if your application is using Erlang, consider using a proplist when storing the objects. If you were to store each value like
[{<<"age">>,24},{<<"name">>,<<"krishna">>}],
the return from your MR might look something like:

{ok,[{0, 
     [{<<"age">>, 24}, {<<"name">>, <<"krishna">>}],
     [{<<"age">>, 29}, {<<"name">>, <<"sharat">>}],
     [{<<"age">>, 25}, {<<"name">>, <<"ramesh">>}]}]}

Then you could use something like
[ {proplists:get_value(<<"name">>,V),proplists:get_value(<<"age">>,V) || V <- R ].
to get back:

[{<<"krishna">>,24},
 {<<"sharat">>,29},
 {<<"ramesh">>,25}]

To extract a specific field from a specific record you could use:

[ "age: " ++ integer_to_list(proplists:get_value(<<"age">>,Record) || 
      Record <- R, proplists:get_value(<<"name">>,Record) =:= <<"krishna">> ].

Also keep an eye out for an R17 compatible client, you may find Erlang's new map data type useful

Joe
  • 25,000
  • 3
  • 22
  • 44