Wednesday, August 17, 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
  • Replace Line 21 "if NOT DEFINED CASSANDRA_HOME set CASSANDRA_HOME=%CD%" with "if NOT DEFINED CASSANDRA_HOME set CASSANDRA_HOME=%~dp0.."
    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.


package com.examples.cassandra;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
import me.prettyprint.hector.api.beans.Rows;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.MultigetSliceQuery;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SliceQuery;

public class CassandraExample {

	//The string serializer translates the byte[] to and from String using utf-8 encoding
    private static StringSerializer stringSerializer = StringSerializer.get();
	
    public static void insertData() {
    	try {
     		//Create a cluster object from your existing Cassandra cluster
            Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160");
           
            //Create a keyspace object from the existing keyspace we created using CLI
            Keyspace keyspace = HFactory.createKeyspace("AuthDB", cluster);
           
            //Create a mutator object for this keyspace using utf-8 encoding
            Mutator<string> mutator = HFactory.createMutator(keyspace, stringSerializer);
    		
            //Use the mutator object to insert a column and value pair to an existing key
            mutator.insert("sample", "authCollection", HFactory.createStringColumn("username", "admin"));
            mutator.insert("sample", "authCollection", HFactory.createStringColumn("password", "admin"));
           
            System.out.println("Data Inserted");
            System.out.println();
    	} catch (Exception ex) {
    		System.out.println("Error encountered while inserting data!!");
    		ex.printStackTrace() ;
    	}
    }
   
    public static void retrieveData() {
    	try {
    		//Create a cluster object from your existing Cassandra cluster
            Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160");
           
            //Create a keyspace object from the existing keyspace we created using CLI
            Keyspace keyspace = HFactory.createKeyspace("AuthDB", cluster);
    		SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
            sliceQuery.setColumnFamily("authCollection").setKey("sample");
            sliceQuery.setRange("", "", false, 4);
           
            QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
            System.out.println("\nInserted data is as follows:\n" + result.get());
    		System.out.println();
    	} catch (Exception ex) {
    		System.out.println("Error encountered while retrieving data!!");
    		ex.printStackTrace() ;
    	}
    }
   
    public static void updateData() {
    	try {

    		//Create a cluster object from your existing Cassandra cluster
            Cluster cluster = HFactory.getOrCreateCluster("Test Sample", "localhost:9160");
           
            //Create a keyspace object from the existing keyspace we created using CLI
            Keyspace keyspace = HFactory.createKeyspace("AuthDB", cluster);
           
          //Create a mutator object for this keyspace using utf-8 encoding
            Mutator<string> mutator = HFactory.createMutator(keyspace, stringSerializer);
           
            //Use the mutator object to update a column and value pair to an existing key
            mutator.insert("sample", "authCollection", HFactory.createStringColumn("username", "administrator"));
           
            //Check if data is updated
            MultigetSliceQuery<String, String, String> multigetSliceQuery = HFactory.createMultigetSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
            multigetSliceQuery.setColumnFamily("authCollection");
            multigetSliceQuery.setKeys("sample");
       
            //The 3rd parameter returns the columns in reverse order if true
            //The 4th parameter in setRange determines the maximum number of columns returned per key
            multigetSliceQuery.setRange("username", "", false, 1);
            QueryResult<Rows<String, String, String>> result = multigetSliceQuery.execute();
            System.out.println("Updated data..." +result.get());
    		
    	} catch (Exception ex) {
    		System.out.println("Error encountered while updating data!!");
    		ex.printStackTrace() ;
    	}
    }

    public static void deleteData() {
    	try {
   
    		//Create a cluster object from your existing Cassandra cluster
    	       Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160");
    	      
    	       //Create a keyspace object from the existing keyspace we created using CLI
    	       Keyspace keyspace = HFactory.createKeyspace("AuthDB", cluster);
    	      
    	     //Create a mutator object for this keyspace using utf-8 encoding
    	       Mutator<string> mutator = HFactory.createMutator(keyspace, stringSerializer);
    	      
    	       //Use the mutator object to delete row
    	       mutator.delete("sample", "authCollection",null, stringSerializer);
    	      
    	       System.out.println("Data Deleted!!");
    	      
    	       //try to retrieve data after deleting
    	       SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
    	       sliceQuery.setColumnFamily("authCollection").setKey("sample");
    	       sliceQuery.setRange("", "", false, 4);
    	      
    	       QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
    	       System.out.println("\nTrying to Retrieve data after deleting the key 'sample':\n" + result.get());
    	      
    	       //close connection
    	       cluster.getConnectionManager().shutdown();
   
		} catch (Exception ex) {
			System.out.println("Error encountered while deleting data!!");
			ex.printStackTrace() ;
		}
	}
   
   
	public static void main(String[] args) {
		
		insertData() ;
		retrieveData() ;
		updateData() ;
		deleteData() ;
       
	}
}
</pre>


The screen shot of the output is as follows:


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

Sunday, August 14, 2011

Error: InvalidRequestException in Cassandra

I have encountered two different kinds of InvalidRequestExceptions while i was working with Cassandra database and Java and I could not find help online.
Fortunately, I could resolve these myself. They are:
  1. InvalidRequestException(why:unconfigured columnfamily sample)
This Exception is encountered if the given column family name is not created in the database. The column family means table in relational database. There are two ways to solve this problem.
  • In java code, you can change the name of the column family to the one existing in the database.
Eg:  If the code is like this,
                 mutator.insert("sample", "auth", HFactory.createStringColumn("username", "admin"));
Change the column family name sample to the one present in the database i.e., if the column family name is login then it should be,
                mutator.insert("login", "auth", HFactory.createStringColumn("username", "admin"));
  •  Create a column family name in the database as follows,
Eg: create column family login ;

  1. InvalidRequestException(why:Keyspace Sample does not exist)

This Exception is encountered if the given Keyspace name is not created in the database. The keyspace means database in relational database. There are two ways to solve this problem.
  • In java code, you can change the name of the Keyspace to the one existing in the database.
Eg:  If the code is like this,
                 Keyspace keyspace = HFactory.createKeyspace("Sample", cluster);
Change the column family name sample to the one present in the database i.e., if the Keyspace name is Login then it should be,
                Keyspace keyspace = HFactory.createKeyspace("Login", cluster);
  •  Create a keyspace in the database as follows,
Eg: create keyspace login ;









Monday, August 8, 2011

A Simple Read/Write Example using MongoDB and Java

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

We only use Eclipse IDE.

To find out more about NoSQL databases, look here.

MongoDB is a scalable, high-performance open source, schema free, Document-oriented NoSQL database and is highly suitable for Web Applications.

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


  • Download and Install MongoDB from here.
  • Extract the files.
  • Run the mongod.exe file present in the bin folder of the Mongodb folder to start the server.
  • If you want to open the UI for the database, run the mongo.exe file present in the bin folder.
  • You will be needing a mongo.jar file to connect Java to the MongoDB which can be downloaded from here.
  • This jar file is also 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 this jar file to the class path.


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

package com.examples.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo ;

public class MongoDBExample {

  public static void main(String[] args) {
  
   try {
    // Mongodb Database Setup
    Mongo m = new Mongo("localhost", 27017) ;
    DB db = m.getDB("authDB") ;
    DBCollection col = db.getCollection("authCollection") ;
  
    // Creating Data 
    // setup and insert data into the authDB database

    BasicDBObject doc = new BasicDBObject() ;
    doc.put("username", "admin") ;
    doc.put("password", "admin") ;
    System.out.println(doc);
    col.insert(doc) ;
   
    // 5 records  are inserted in the following 'for' loop
    for (int i = 0 ; i < 5 ; i++) {
     BasicDBObject data = new BasicDBObject() ;
     data.put("username", "user_" + i ) ;
     data.put("password", "pass_" + i ) ;
     System.out.println(data);
     col.insert(data) ;
     data.clear() ;
    }
    System.out.println("Data Inserted");
   
    // Retrieving Data 
    System.out.println("Data Retrieved");
    DBCursor cursor = col.find() ;
    while (cursor.hasNext()) {
     // The entire records in the collection is displayed here.
     System.out.println(cursor.next());
    }
    

    // Updating Data 
    BasicDBObject oldData = new BasicDBObject() ;   
    oldData.put("username", "admin") ;
    oldData.put("password", "admin") ;
   
    BasicDBObject newData = new BasicDBObject() ;
    newData.put("username", "administrator") ;
    newData.put("password", "administrator") ;
    col.update(oldData, newData) ;
    System.out.println("Data Updated");
   
    DBCursor cursor1 = col.find() ;
    while (cursor1.hasNext()) {
     // The updated record with other records present in the collection is displayed here.
     System.out.println(cursor1.next());
    }
   
    // Deleting Data 
    // This deletes the record where username equals user_5
    BasicDBObject delRecord = new BasicDBObject() ;
    delRecord.put("username", "user_5") ;
    col.remove(delRecord) ;
    System.out.println("Record Deleted") ;
   
    DBCursor cursor2 = col.find() ;
    while (cursor2.hasNext()) {
     // Here there is no record with username data equals user_5
     System.out.println(cursor2.next());
    }

    // Dropping or deleting entire database
    m.dropDatabase("authDB") ;
    System.out.println("Database deleted/dropped");
    DBCursor cursor3 = col.find() ;
    while (cursor3.hasNext()) {
     //Nothing is displayed here as the database is deleted
     System.out.println(cursor3.next());
    }
   } catch (Exception e) {
    // TODO: handle exception
    System.out.println(e.getMessage()) ;
    e.printStackTrace() ;
   }
  }
}


The screen shot of the output is as follows:



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