In this post I am doing  establish a connection to cassandra from java, how to  read the values from Cassandra database, how to  update the values from cassandra database and delete the values from cassandra using java.

The basic steps are doing CRUD operations in Cassandra. First create the keyspace in Cassandra using CQLSH.
  •    I am creating a sample keyspace in Cassandra using CQLSH.
CREATE KEYSPACE sample WITH replication = { ‘class’:  'SimpleStrategy' ,  'replication_factor' :  '3' };
                   In the above keyspace creation I am taking class as a SimpleStrategy because I am using in local, if you are doing in production take the NetworkTopologyStrategy instead of ‘SimpleStrategy’.
  •    Create a table in Sample keyspace using CQLSH.
CREATE TABLE usersdata (
  user_name        text,
  no_of_friends   bigint,
  no_of_shared_posts   bigint,
  PRIMARY KEY (user_name)
) ;

Sample snippet of  usersdata table in CQLSH

  • 3)      Create a MAVEN project using eclipse
Maven POM  for  Cassandra-driver-core .You need to add the fallowing dependency in POM.xml file.

In this dependency i am using the 2.1.3 version. 

  • 4)      Example program on CRUD operations in Cassandra using java 

package com.spark.cassandra;

import java.util.List;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

 * @author Venkata Ramu Kandulapati
 * Created on Dec 23, 2014 4:59:07 PM
public class CassandraConnection {
       public static Session session = null;
       public static final String KEYSPACE = "sample";
       public static final String TABLE_USERS_DATA = "usersdata";
       public static  PreparedStatement insertPstmt = null;
       public static PreparedStatement  selectStmt  = null;
       public static PreparedStatement deleteStmt       = null;
       public static PreparedStatement updateStmt      = null;
       public static final String USER_DATA_INSERT_STMT = "INSERT INTO " + KEYSPACE   +"."+ TABLE_USERS_DATA +"(user_name,no_of_friends,no_of_shared_posts)values(?,?,?)";
       public static final String USER_DATA_SELECT_STMT = "SELECT * FROM " + KEYSPACE +"."+TABLE_USERS_DATA;
       public static final String UPDATE_STMT ="UPDATE "+ KEYSPACE +"."+TABLE_USERS_DATA+" SET no_of_shared_posts = ?  where user_name= ? ";
       public static final String USER_DATA_DELETE_STMT = "DELETE FROM " + KEYSPACE +"."+TABLE_USERS_DATA +" WHERE user_name = ?";
       public CassandraConnection() {
              String serverIp ="";
              Cluster cluster = Cluster.builder()
              session = cluster.connect(KEYSPACE);
              insertPstmt = session.prepare(USER_DATA_INSERT_STMT);
              selectStmt = session.prepare(USER_DATA_SELECT_STMT);
              updateStmt = session.prepare(UPDATE_STMT);
              deleteStmt = session.prepare(USER_DATA_DELETE_STMT);
       public void insertValues() {
              System.out.println("Succesfully inserted the data in cassandra table");
       public void retriveValues() {
              try {
                     ResultSet rs = session.execute(selectStmt.bind());
                     if(rs != null) {
                           List<Row> rows =rs.all();
                           if(rows != null) {
                                  for(Row row : rows) {
              } catch(Exception e) {
                     System.out.println("Exception in select Stament"+e.getMessage());
       public void deleteValues() throws Exception{
              try {
              } catch(Exception e) {
                     throw e;
       public void updateValues() throws Exception {
              try {
              } catch(Exception e) {
                     throw e;
       public static void main(String args[]) throws Exception {
              try {
                     CassandraConnection cc = new CassandraConnection();
              } catch(Exception e) {
                     System.out.println("Exception in the CRUD operations"+e);

After running the above program get the fallowing output,I ran the above program without deleteValues method.


Post a Comment