Querying Examples

Jason Tanner jt4websites at googlemail.com
Thu Feb 4 13:03:31 EST 2010


I had a reply to my question from a list member, but as it was direct to me
I thought I'd repost it back to the list so that future users with a similar
question  can benefit from the answer.


Your map function (in Erlang or JS) needs to return a list of the mapped
values, so if you are filtering by age, write a function like this:

FilterAgeLessThan = fun(O,_,Limit) ->
   PList = riak_object:get_value(O),
   case proplists:get_value(age, PList) of
       A when A < Limit -> [O];
       _ -> []

Then that phase can be created like so:

{map, {qfun, FilterAgeLessThan}, 50, false}

You would create a similar function for your home_town property, or
generalize the above to take any property/value combination, and then create
one for equality.  You shouldn't need a reduce phase for that query.


On 3 February 2010 22:57, Jason Tanner <jt4websites at googlemail.com> wrote:

> Hi,
> Im trying to work out how, using riak's *erlang* *map reduce* *Interface*I would do something equivalent to the following SQL:
>     FROM Users
>  WHERE age < 50
>       AND  home_town = "London"
> So assuming I had a bucket "Users" keyed on some unique user-id where the
> object value associated with the users key was structured something like:
> [ {first_name, "Bob"},
>   {last_name, "Smith" },
>   {age,           30 },
>   {home_town, "London" } ]
> (Lets also assume that the "Users" bucket contains several thousand user
> key/value entries, and I want to produce a subset of them based on the
> filter criteria above in the SQL)
> What could my erlang map function look like ?
> What could my erlang reduce function look like ?
> Unfortunately, all the examples I have been able to find, seem to be
> related to counting and accumulating entries.
> Regards,
> Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20100204/e7599866/attachment.html>

More information about the riak-users mailing list