Skip to content

Instantly share code, notes, and snippets.

@ksauzz
Created September 5, 2013 03:19
Show Gist options
  • Select an option

  • Save ksauzz/6445666 to your computer and use it in GitHub Desktop.

Select an option

Save ksauzz/6445666 to your computer and use it in GitHub Desktop.
riak-client-java counter sample
import com.basho.riak.client.IRiakClient;
import com.basho.riak.client.RiakFactory;
import com.basho.riak.client.bucket.Bucket;
import com.basho.riak.client.raw.config.Configuration;
import com.basho.riak.client.raw.http.HTTPClientConfig;
import com.basho.riak.client.raw.pbc.PBClientConfig;
public class RiakCounterSample {
private static final int MAX_CONNECTION_SIZE = 10;
private static final int INIT_CONNECTION_SIZE = 10;
private static final int BUFFER_KB = 16;
private static final int IDLE_CONN_TIMEOUT_MIL = 2000;
private static final int CONNECTION_TIMEOUT_MIL = 2000;
private static final int REQUEST_TIMEOUT_MIL = 2000;
private static final String BUCKET_NAME = "counter_demo";
private static final String COUNTER_KEY = "counter";
public static void main(String[] args) throws Exception {
// Configuration conf = PBClientConfig.defaults();
// Configuration conf = HTTPClientConfig.defaults();
Configuration conf = new PBClientConfig.Builder()
.withHost("127.0.0.1")
.withPort(8087)
.withConnectionTimeoutMillis(CONNECTION_TIMEOUT_MIL)
.withIdleConnectionTTLMillis(IDLE_CONN_TIMEOUT_MIL)
.withSocketBufferSizeKb(BUFFER_KB)
.withRequestTimeoutMillis(REQUEST_TIMEOUT_MIL)
.withInitialPoolSize(INIT_CONNECTION_SIZE)
.withPoolSize(MAX_CONNECTION_SIZE)
.build();
IRiakClient client = RiakFactory.newClient(conf);
Bucket bucket = client.createBucket(BUCKET_NAME).allowSiblings(true).execute();
for (int i = 0; i < 20; i++) {
bucket.counter(COUNTER_KEY).increment(5L).execute(); // increment counter
log("Counter: " + bucket.counter(COUNTER_KEY).execute()); // fetch counter
}
for (String key : bucket.keys()) {
bucket.delete(key).execute();
}
client.createBucket(BUCKET_NAME).allowSiblings(false).execute();
client.shutdown();
}
private static void log(String log) {
System.out.println(log);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment