This simple demonstrator that makes a connection to a Cassandra cluster and inserts a user-defined number of row, then extracts them to demonstrate performance boost with multiGet.
It’s genesis was the result of working towards a Thrift API loader for a Cassandra evaluation implementation. You can read bout that, here: Cassandra – A Use case examined (IP data)
/*
------------------------------------------------------------------------
$Id: useMultiGet.java,v 1.2 2012/03/15 12:56:45 ddemartini Exp $
$Revision: 1.2 $
$Date: 2012/03/15 12:56:45 $
$Name: $
------------------------------------------------------------------------
MultiGet demonstrator
This class makes use of our custom Utility class
------------------------------------------------------------------------
*/
package c01;
import base.*;
import java.util.*;
import java.nio.ByteBuffer;
import org.apache.cassandra.thrift.*;
public class useMultiGet {
public static void main (String[] args) throws Exception {
/* Use to specify host,port,keyspace,columnfamily and insert count */
String host = Util.envOrProp("host");
int port = Integer.parseInt(Util.envOrProp("port"));
int inserts = Integer.parseInt(Util.envOrProp("inserts"));
String ks = Util.envOrProp("ks");
String cf = Util.envOrProp("cf");
int slice = Integer.parseInt(Util.envOrProp("slice"));
/* instance db connector and open connection */
CassDB db = new CassDB(host,port);
db.open();
/* first check for the existance of the keyspace */
/* use ColumnParent to insert data */
ColumnParent parent = new ColumnParent();
parent.setColumn_family(cf);
/* use ColumnPath to get data */
ColumnPath path = new ColumnPath();
path.setColumn_family(cf);
path.setColumn("acol".getBytes("UTF-8")); /* RESEARCH */
/* use defined the number of inserts to make in this demonstrator */
Column col = new Column();
db.getClient().set_keyspace(ks);
col.setName("acol".getBytes());
long timestamp = System.currentTimeMillis();
for (int j = 0; j < inserts; j++) {
ByteBuffer key = ByteBuffer.wrap((j+"").getBytes());
col.setValue(key);
col.setTimestamp(timestamp); /* you MUST add a timestamp!! */
col.setTtl(600); /* set a TTL 10 minutes into the future */
db.getClient().insert(key, parent, col, ConsistencyLevel.ALL); /* insert! */
db.getClient().get(key, path, ConsistencyLevel.ALL); /* get data back */
}
/* create a timer */
long getNanos = System.nanoTime();
for (int j = 0; j < inserts; j++) {
ByteBuffer key = ByteBuffer.wrap((j+"").getBytes());
col.setValue(key);
db.getClient().get(key, path, ConsistencyLevel.ONE);
}
long endGetNanos = System.nanoTime()-getNanos;
/* se MultiGet, requires a SlicePredicate which can either be a list of columns or a slice range */
SlicePredicate pred = new SlicePredicate();
pred.addToColumn_names(ByteBuffer.wrap("acol".getBytes()));
long startMgetNanos = System.nanoTime();
/* loop in batches of 5 */
for (int j = 0; j < inserts; j=j+5){
List
1 comment for “Java multi-get demonstrator for Cassandra NoSQL db”