Counters inside Maps

Alex De la rosa alex.rosa.box at gmail.com
Tue Aug 19 18:38:37 EDT 2014


Hi Luc,

Thanks for your link to the documentation; that was exactly what I saw on
the data dump some time ago, didn't know it was published there :)

Just one question regarding your example in contrast with Sean's...

x.registers['name'].assign('Messi')
x.registers['name_s'].assign('Messi')

You added "_s" to the field while Sean removed it and said it will be
"name_register" automatically... in your case would turn into
"name_s_register" I supposed, right?

Thanks!
Alex


On Wed, Aug 20, 2014 at 12:27 AM, Luc Perkins <lperkins at basho.com> wrote:

> Alex,
>
> Do you mean a map as in a Python dict or as in a Riak map? If you want to
> create *footballers* as a Riak map, skip to the *Maps* section in this doc
> <http://docs.basho.com/riak/2.0.0/dev/using/data-types/#Maps>. That
> provides a full tutorial for dealing with other Data Types, such as
> counters, within maps, and has Python code samples.
>
> If you're storing Python dicts as JSON in Riak, as in your example, you
> will have to GET the map from Riak and manipulate it client side. So if you
> wanted to add a *goals_i* counter that starts at 0, you'd have to do
> something like this:
>
> *obj = bucket.get('lionelmessi')*
> *obj.data['goals_i'] = 0*
> *obj.store()*
>
> To increment the counter, you could do so in a function like this:
>
> *def increment(bucket, key, n):*
> *    obj = bucket.get(key)*
> *    obj['goals_i'] = obj['goals_i'] + n*
> *    obj.store()*
>
> That's of course a very rough example. But you can probably see from this
> example that using Riak maps can be a very nice option if you're using a
> data model that fits. Here's an example of storing your *lionelmessi* object
> as a Riak map:
>
> *riak-admin bucket-type create maps '{"props":{"datatype":"map"}}'*
> *riak-admin bucket-type activate maps*
>
> *from riak.datatypes import Map*
>
> *bucket = client.bucket_type('maps').bucket('footballers')*
> *messi_map = Map(bucket, 'lionelmessi')*
> *messi_map.registers['name_s'].assign('Messi')*
> *messi_map.registers['team_s'].assign('Barcelona')*
> *messi_map.registers['number_i'].assign(str(10))*
> *messi_map.counters['goals_i'].increment()*
> *messi_map.store()*
>
> Luc
>
>
> On Tue, Aug 19, 2014 at 2:59 PM, Alex De la rosa <alex.rosa.box at gmail.com>
> wrote:
>
>> Imagine I have a Riak object "footballer" with some static fields: name,
>> team, number. I store them like this now:
>>
>> 1: CREATE INDEX FOR RIAK SEARCH
>> curl -XPUT "http://148.251.140.229:8098/search/index/ix_footballers"
>>
>> 2: CREATE BUCKET TYPE
>> riak-admin bucket-type create tp_footballers
>> '{"props":{"allow_mult":false,"search_index":"ix_footballers"}}'
>> riak-admin bucket-type activate tp_footballers
>>
>> 3: INSERT A PLAYER
>> bucket = client.bucket_type('tp_footballers').bucket('footballers')
>> key = bucket.new('lionelmessi', data={'name_s':'Messi',
>> 'team_s':'Barcelona', 'number_i':10}, content_type='application/json')
>> key.store()
>>
>> 4: SEARCH FOR BARCELONA PLAYERS
>> r = client.fulltext_search('ix_footballers', 'team_s:Barcelona')
>>
>> So far so good :) BUT... what if I want to have a field "goals_i" that is
>> a counter that will be incremented each match day with the number of goals
>> he scored? What is the syntax/steps to do to set up "footballers" as a MAP
>> and then put a COUNTER inside? I know is possible as I read it in some data
>> dump some Basho employee passed me some time ago, but I can't manage to see
>> how to do it now.
>>
>> Thanks!
>> Alex
>>
>> _______________________________________________
>> riak-users mailing list
>> riak-users at lists.basho.com
>> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20140820/bde74205/attachment.html>


More information about the riak-users mailing list