2011. január 11., kedd

Apache Cassandra 0.7 example

Ezt a bejegyzést kivételesen angolul teszem meg.

It seems that the newest Apache Cassandra doesn't want to work out of box. Therefore, I write down this instruction how to run a Hello World application on a single-node out-of box Cassandra 0.7 installation.

Step 1.

Install cassandra.

Step 2.

In conf/cassandra.yaml file, change the

  • data_file_directories

  • commitlog_directory

  • saved_caches_directory


entries.

Step 3.

Despite the conf/cassandra.yaml contains a default definition for Keyspace1 keyspace and Standard1 column factory, it doesn't seem to use it when you start the bin/cassandra.bat -f.

In order to fix this, you need to manually (or programmatically) create the Keyspace1 and Standard1 columns. You can do this by running the bin/cassandra-cli --host localhost and executing the following commands:

create keyspace Keyspace1;

use Keyspace1;

create column family Standard1 with comparator=BytesType;

Step 4.

Now you can write the java application:

Connect to the local instance

TTransport framedTransport = new TFramedTransport(new TSocket("localhost", 9160));
TProtocol framedProtocol = new TBinaryProtocol(framedTransport);
Cassandra.Client client = new Cassandra.Client(framedProtocol);
framedTransport.open();


Start using Keyspace1:

client.set_keyspace("Keyspace1");

Add a new entry:


ColumnParent colParent = new ColumnParent("Standard1");

client.insert(
ByteBuffer.wrap("TheRowKey".getBytes("UTF-8")),
colParent,
new Column(
ByteBuffer.wrap("columnName1".getBytes("UTF-8")),
ByteBuffer.wrap("value1".getBytes("UTF-8")),
System.currentTimeMillis()
)
ConsistencyLevel.ONE
);


Get that column back:


ColumnPath colPath = new ColumnPath("Standard1");
colPath.setColumn(ByteBuffer.wrap("columnName1".getBytes("UTF-8")));

ColumnOrSuperColumn col = client.get(
ByteBuffer.wrap("TheRowKey".getBytes("UTF-8")),
colPath,
ConsistencyLevel.ONE
);

System.out.println(col.column);
System.out.println(new String(col.column.getName(), "UTF-8") + " -> " + new String(col.column.getValue(), "UTF-8"));