Riak Java client counter object

Dave Rusek drusek at basho.com
Fri Jan 24 17:01:15 EST 2014


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 = ...


DtFetchOperation get = 
	new DtFetchOperation.Builder(bucket, key)

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

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


DtUpdateOperation update = 
	new DtUpdateOperation.Builder(bucket, key)
		.withOp(new CounterOp(1)) // increment by 1

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

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


long value = fetch();


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

[1] https://github.com/basho/riak-java-client 

Dave Rusek
Basho Technologies

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


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)

riak-users mailing list  
riak-users at lists.basho.com  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20140124/c0ff3c16/attachment.html>

More information about the riak-users mailing list