Go back to Table of Contents
So you know that Riak stores things in buckets and that these things are key-value pairs. We will go over these basics again. Start by launching Erlang :
erts-5.7.4/bin/erl -name riaktest -setcookie riak
: which should take you to the Erlang shell. Then enter :
{ok, RiakClient} = riak:client_connect('riak@192.168.1.4').
: which should return something like :
{ok,{riak_client,'riak@192.168.1.4',<<7,148,98,13>>}}
: Then see what is in Riak ( assuming you have already done the storing things in Riak tutorial) :
RiakClient:list_buckets( ).
: which should return :
{ok,[<<"right_hand_bucket">>,
<<"left_hand_bucket">>]}
Lets create a new Bucket called all_sorts_of_things_in_here with a favorite films list in it :
FavoriteFilmsItem =
riak_object:new(<<"all_sorts_of_things_in_here">>, <<"favorite_films">>, ["star wars"]).
RiakClient:put( FavoriteFilmsItem , 1).
: and if successful we get back :
ok
Then, just to show that the new bucket has been created :
RiakClient:list_buckets( ).
: should result in something like :
{ok,[<<"right_hand_bucket">>,
<<"all_sorts_of_things_in_here">>,
<<"left_hand_bucket">>]}
<<"all_sorts_of_things_in_here">>,
<<"left_hand_bucket">>]}
Then check the what is in this new bucket :
RiakClient:list_keys(<<"all_sorts_of_things_in_here">>).
: which should return :
{ok,[<<"favorite_films">>]}
Then to read back the value of favorite films use :
{ ok, FavoriteFilmsItem2 } = RiakClient:get(
<<"all_sorts_of_things_in_here">>,
<<"favorite_films">>,
1).
<<"all_sorts_of_things_in_here">>,
<<"favorite_films">>,
1).
FavoriteFilmsItem2.
: and you will get the value of FavoriteFilmsItem2 as :
{r_object,<<"all_sorts_of_things_in_here">>,
<<"favorite_films">>,
[{r_content,{dict,2,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],[[...]],[],...}}},
["star wars"]}],
[{<<7,148,98,13>>,{1,63433803807}}],
{dict,1,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],[],[],[],...}}},
undefined}
: so what does all this mean? Well, maybe its best just to get the value out for now:
FavoriteFilmsValue = riak_object:get_value( FavoriteFilmsItem2 ).
: which should return :
["star wars"]
So, what if we have another film we want to add to out list of favorites? Well, first we need a new list with the new film in it. So we do this in Erlang with :
UpdatedFavoriteFilmsValue = ["Terminator" | FavoriteFilmsValue].
so the new list can be seen by entering:
UpdatedFavoriteFilmsValue.
: returning :
["Terminator","star wars"]
Then we need to do the following:
UpdatedFavoriteFilmsItem = riak_object:update_value(
FavoriteFilmsItem,
UpdatedFavoriteFilmsValue).
FavoriteFilmsItem,
UpdatedFavoriteFilmsValue).
RiakClient:put( UpdatedFavoriteFilmsItem, 1).
: and the Erlang command prompt should indicate success with :
ok
now if you look at the favorite films:
{ ok, UpdatedFavoriteFilmsItem2 } = RiakClient:get(
<<"all_sorts_of_things_in_here">>,
<<"favorite_films">>, 1).
<<"all_sorts_of_things_in_here">>,
<<"favorite_films">>, 1).
riak_object:get_value( UpdatedFavoriteFilmsItem2 ).
: shows :
["Terminator","star wars"]
Next, if we want to delete the list of films:
RiakClient:delete(<<"all_sorts_of_things_in_here">>, <<"favorite_films">>,1).
: should return :
ok
: and then confirm that :
RiakClient:list_keys( <<"all_sorts_of_things_in_here">> ).
: returns :
{ok,[ ]}
Also, try:
RiakClient:list_buckets( ).
: should return :
{ok,[<<"right_hand_bucket">>,
<<"left_hand_bucket">>]}
This shows that the bucket all_sorts_of_things_in_here does not exist anymore either. This is because buckets in Riak are really just a prefix to keys and unlike in real life, buckets in Riak cannot exists without any keys in them.
So in this article you have used the following new commands:
{ ok, UpdatedFavoriteFilmsItem } = RiakClient:get(<<"all_sorts_of_things_in_here">>, <<"favorite_films">>, 1).
UpdatedFavoriteFilmsValue = ["Terminator" | FavoriteFilmsValue].
TheFilmsValue = riak_object:get_value( TheFilms ).
UpdatedFavoriteFilmsObject = riak_object:update_value(FavoriteFilmsObject, TheNewListOfFilms).
RiakClient:delete(<<"all_sorts_of_things_in_here">>, <<"favorite_films">>,1).
No comments:
Post a Comment