Structure of riak_object:get_value result

Kresten Krab Thorup krab at
Thu Apr 28 18:18:11 EDT 2011

Hi Michail, ...

1. If you stored the object via the HTTP or PB interfaces, then then object will simply be a binary corresponding to the body content you sent to the service.  So, if it's JSON then you need to parse it.  You can use Dict = riak_object:get_metadata/1 to get a dict that roughly corresponds to the HTTP headers (most notably the Content-Type can be seen there).  When using the native Erlang API, you can store any Erlang term as the "value" of a riak_object.

2. If you enable allow_mult=true on your bucket, then you need to use get_values / get_metadatas, because a write conflict could make your object have multiple values.

At any rate, you'll already have a JSON decoder (mochijson2) when running inside Riak; so you can do

	JSON = mochijson2:decode(Value)

To decode your JSON.  For the JSON document {"key":4, "val":{"x"="y"}} , that function should return 

 {struct, [{<<"key">>, 4}, 
           {<<"val">>, {struct, 
                         [{<<"x">>, <<"y">>}]}]}

... and you should be good to go.

On Apr 28, 2011, at 11:50 PM, Mikhail Sobolev wrote:

> Hi,
> I'm trying to write a map function in Erlang.  As far as I understand,
> the function must have arity 3 (according to the wiki) and the first
> parameter is the actual object (corresponding to the given bucket/key).
> To obtain the value of the object, I use riak_object:get_value function,
> however I'm not sure about the structure of that value (wiki does not
> seem to say anything about it).
> Could somebody point me to the right place to look at?
> Essentially, I'd be looking for sometihng that says: "here's a JSON data
> structure, and this is how it looks an Erlang term".
> --
> Misha
> <signature.asc><ATT00001..txt>

More information about the riak-users mailing list