Erlang Client Example for using links in map/reduce

Bryan Fink bryan at basho.com
Sat Oct 10 09:41:04 EDT 2009


On Sat, Oct 10, 2009 at 8:39 AM, Paul Jones <pauljones23 at gmail.com> wrote:
> I was wondering if anyone could post a code example of how you might
> structure the data inside a value so you can execute a linked map/reduce?
> The presentation covers what the search code would look like, but I'm not
> really clear what I'd put inside my value.

You're right - there's a distinct lack of examples around actually
setting up the links.  I'll try to explain in this email, and
hopefully I'll also remember to commit some of it to the unreleased
0.6 version.

I'll describe how to manage objects you have requested over the HTTP
interface, since I expect that's what most people are dealing with.
If you look at the object you retreive from Riak, you'll find that in
addition to 'bucket', 'key', and 'object' fields, there is also a
'links' field.  This 'links' field is a JSON array of links.

To add links to an object, add the new links to the 'links' field's
array, and store the object back to Riak.  To remove links, remove
them from this array, and store the object back to Riak.

The format of a link is a length-3 JSON array with element 0 as the
bucket name, element 1 as the value's key, and element 2 as the tag.
So the link:

    ["album", "green", "solo"]

Would be a link to the object in the "album" bucket with the key
"green".  The link is tagged "solo".  So, if you were to store an
object like:

    {'bucket':'artist',
     'key':'abc',
     'object':{...},
     'links':[ ['album', 'green', 'solo'] ]}

any of the following HTTP GETs would return you the album/green object:

    http://host/jiak/artist/abc/_,_,_
    http://host/jiak/artist/abc/album,_,_
    http://host/jiak/artist/abc/_,solo,_
    http://host/jiak/artist/abc/album,solo,_

If you were to modify and store the object like:

    {'bucket':'artist',
     'key':'abc',
     'object':{...},
     'links':[
              ['album', 'green', 'solo'],
              ['album', 'blue', 'duet']
             ]}

Then you could get both the album/blue and album/green objects with:

    http://host/jiak/artist/abc/_,_,_
    http://host/jiak/artist/abc/album,_,_

Or just the album/blue object with:

    http://host/jiak/artist/abc/_,duet,_
    http://host/jiak/artist/abc/album,duet,_

I recommend checking out the comments on jiak_object.erl, or the
Javascript from the stickynotes demo application (especially the click
handler for 'div.add' near the bottom of the file) for more examples:

http://bitbucket.org/justin/riak/src/tip/src/jiak_object.erl
http://bitbucket.org/justin/riak/src/tip/demo/stickynotes/priv/www/js/application.js

For now, I'll just give a clue about how you could support links in an
entirely different format of data inside Riak (using the raw-Erlang
interface instead of the HTTP interface): grep linkfun src/*.erl.  If
someone is really interested in such things, and needs a few more
pointers, I'll write a second how-to.

-Bryan




More information about the riak-users mailing list