Sunday, August 14, 2011

A Simple Read/Write Example using Cassandra and Java

In this post, we take a look at basic CRUD (Create Read Update Delete) operations on Cassandra database using Java JDBC.

We only use Eclipse IDE. This example was tested in Windows 7 Operating System.

To find out more about NoSQL databases, look here.

Cassandra is an open source distributed database management system developed by Apache Software Foundation. It was designed to handle very large amounts of data spread out across many commodity servers while providing a highly available service with no single point of failure.
Popular user of Cassandra is Facebook

To find out more on Cassandra, refer its home site here.

  • Download and Install Cassandra from here.
  • Extract the files.
  • Setup the following Environment Variables.
    • JAVA_HOME (Give the link to the jre folder where java is installed)
    • CASSANDRA_HOME (Give the link to the cassandra folder where the files are extracted)
  • Go to the location where you extracted and then into the "~/apache-cassandra-0.6.6/bin" folder and open 'cassandra-cli.bat' file using any text editor
    Now save the file after editing the line.
  • Now open the command prompt and goto "~/apache-cassandra-0.6.6/bin>" folder and type the following command '.\cassandra.bat'. This starts the server.
  • Now open another command prompt and goto "~/apache-cassandra-0.6.6/bin>" folder and run '.\cassandra-cli.bat'. This is the UI for the database
  • Connect to the server by typing the command 'connect localhost/9160'.
  • Here, type "Create Keyspace AuthDB ;" This creates a cluster (i.e., database)
  • Here, type "Create Column Family AuthCollection ;" This creates a column family (i.e., table)
  • There are certain jar files needed which are available in the Project Dump which is available for download below. It is present in the folder titled 'JarFiles' in the Project Directory.
  • Be sure to add these jar files to the class path.

The code which performs all the CRUD operations is as follows.

The screen shot of the output is as follows:

This code is available for download along with the entire project directory here.


  1. Hi,
    I have some problems with this app..
    Why problaby my app can't conect with Cassandra?
    Conection? JARs ?
    Thanks, this post save my graduation project!

  2. Great post. was struggling to get started with cassandra to get connected to the local server. the command connect localhost/9160 needs a semicolon at the end for it to work.

  3. Nice post , Thanks Sriram. A nice guide for someone to get started with Cassandra
    A couple of changes to make things work in the above program -
    1. connect localhost/9160 needs a semicolon at the end.
    2. AuthCollection in the program is written with a small a - authCollection. Change that to get started.
    Thanks much