Erlang Client Example for using links in map/reduce

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

On Sat, Oct 10, 2009 at 8:39 AM, Paul Jones <pauljones23 at> 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:

     'links':[ ['album', 'green', 'solo'] ]}

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


If you were to modify and store the object like:

              ['album', 'green', 'solo'],
              ['album', 'blue', 'duet']

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


Or just the album/blue object with:


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:

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.


More information about the riak-users mailing list