Using the C++ Interface with Cassandra
Before starting, Cassandra needs to be downladed and installed. In a previous post, I
went through the steps involved in setting up a Cassandra cluster so I'm not going to repeat that
here. For this simple example though, I'll be using the following keyspace (which needs to be
present in the storage-conf.xml file):
Once we have cassandra installed and running, we next need to download thrift from its Apache homepage. I went with the latest stable release which at the time of writing is 0.2.0. Installation from the tarball is pretty straightforward but ensure to run ldconfig after installing thrift.
Once thrift is installed, we need to generate the C++ interface for Cassandra (this will be done as the cassandra user if following the setup in my previous post):
To compile this, I used the following command line (assuming I am in the cpp-test directory):
Once we have cassandra installed and running, we next need to download thrift from its Apache homepage. I went with the latest stable release which at the time of writing is 0.2.0. Installation from the tarball is pretty straightforward but ensure to run ldconfig after installing thrift.
Once thrift is installed, we need to generate the C++ interface for Cassandra (this will be done as the cassandra user if following the setup in my previous post):
$ cd $CASSANDRA_HOME/interface $ thrift --gen cpp cassandra.thrift $ ls -ltr total 44 drwxr-xr-x 3 cassandra cassandra 4096 2010-02-22 17:57 thrift -rw-r--r-- 1 cassandra cassandra 21105 2010-02-22 17:57 cassandra.thrift -rw-r--r-- 1 cassandra cassandra 3359 2010-02-22 17:57 cassandra.avpr drwxr-xr-x 3 cassandra cassandra 4096 2010-02-22 18:01 avro drwxr-xr-x 2 cassandra cassandra 4096 2010-02-22 21:41 gen-cpp $ mkdir cpp-testWithin the cpp-test directory, I'm going to create a file named simple-test.cc which looks like:
To compile this, I used the following command line (assuming I am in the cpp-test directory):
$ g++ -o cpptest -Wall -g \ > -I../gen-cpp/. \ > -I/usr/local/include/thrift \ > -L/usr/local/lib -lstdc++ -lthrift \ > simple-test.cc \ > ../gen-cpp/cassandra_constants.cpp \ > ../gen-cpp/cassandra_types.cpp \ > ../gen-cpp/Cassandra.cpp $The above command will produce an executable named cpptest in the cpp-test directory. Assuming cassandra is started, we run the binary and should obtain output like so:
$ ./cpptest Column name retrieved is: second Value in column retrieved is: this is data!! $That's a simple example of using the C++ interface to Cassandra. Hopefully, this will prove useful to someone but it took me longer than expected to get the above simple test working so I figured it was worth writing up the steps I went through.