Riak Java client counter object

Dave Rusek drusek at basho.com
Tue Jan 28 11:34:38 EST 2014


Guido,

There is no special way to list the values for all counters in a bucket. You will have to list the keys for the bucket (with a bucket type property ‘datatype=counter’) then fetch the values for those keys (e.g. via multi-fetch). Depending on your use case and the number of counters, you may be able to create a map of counters (‘datatype=map’). This would be an easy way to fetch/update multiple counters in one operation.

-- 
Dave Rusek
Software Engineer
Basho Technologies
@davidjrusek

On January 28, 2014 at 4:29:28 AM, Guido Medina (guido.medina at temetra.com) wrote:

Hi,

Following up on this question, is there a way to list (not multi-fetch, just list) the existing counters in a bucket?

Regards,

Guido.

On 24/01/14 22:01, Dave Rusek wrote:
Guido,

I’m sorry, there are not currently any docs available. Rest assured they are a work in progress. In the meantime, please feel free to check-out the current pre-release [1]. The pre-release covers the low level API we have built on top of the PB interface. There is a start to a friendlier API in the develop branch of riak-java-client [2].

If you would like to get started with the low level API to begin getting a feel for Riak datatypes, I have included a few snippets that will hopefully help. The counters (and the data structures in general) are implemented as two separate operations: Fetch and Update. Updating a counter that does not exist will create it and initialize it to the given value. Fetching a counter that has not had a value stored will return an empty value.

While the low level API does not have convenience methods for getAndIncrement and the like, they can all be easily accomplished given fetch and update. It would clearly be easy for us to provide methods for these typical access patterns and in fact, it is something we are planning on doing.

RiakCluster cluster = …
BinaryValue bucket = …
BinaryValue key = …
BinaryValue type = ...

fetch():

DtFetchOperation get = 
new DtFetchOperation.Builder(bucket, key)
.withBucketType(type)
.build();

DtFetchOperation.Response response = cluster.execute(get).get();

long value;
CrdtElement element = response.getCrdtElement();
if (crdtElement.isCounter()) {
value = element.getCounter().getValue();
} else {
... 
}

update():

DtUpdateOperation update = 
new DtUpdateOperation.Builder(bucket, key)
.withBucketType(type)
.withReturnBody(true)
.withOp(new CounterOp(1)) // increment by 1
.build();

DtUpdateOperation.Response response = cluster.execute(update).get();

long value;
CrdtElement element = response.getCrdtElement();
if (crdtElement.isCounter()) {
value = element.getCounter().getValue();
} else {
... 
}

getAndIncrement();

long value = fetch();
update(1);

incrementAndGet():

long value = update(1); // make sure withReturnBody == true

[1] https://github.com/basho/riak-java-client 
[2]https://github.com/basho/riak-java-client/tree/develop/src/main/java/com/basho/riak/client/operations

-- 
Dave Rusek
Basho Technologies
@davidjrusek

On January 24, 2014 at 8:41:07 AM, Guido Medina (guido.medina at temetra.com) wrote:

Hi,

Is there any small doc that could explain its usage a little bit:

From the Java perspective it would be nice if it point out its counter part methods with Atomic Integer like:
How to create it? Does incrementing a counter will just create it with zero as initial value and then increment it? What if you need to know its value and then increment it before it is created? (getAndIncrement)
get()
getAndIncrement()
incrementAndGet()
Regards,

Guido.
_______________________________________________
riak-users mailing list
riak-users at lists.basho.com
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com

_______________________________________________  
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/20140128/1e3e2a29/attachment.html>


More information about the riak-users mailing list