AdSenseHeader

               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.
<dependency>
              <groupId>com.datastax.cassandra</groupId>
              <artifactId>cassandra-driver-core</artifactId>
              <version>2.1.3</version>
</dependency>


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 ="127.0.0.1";
              Cluster cluster = Cluster.builder()
                           .addContactPoints(serverIp)
                           .build();
              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() {
              session.execute(insertPstmt.bind("Ramu",99L,10L));
              session.execute(insertPstmt.bind("Raju",111L,20L));
              session.execute(insertPstmt.bind("annand",211L,24L));
              session.execute(insertPstmt.bind("Rajesh",333L,33L));
              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) {
                                         System.out.println(row);
                                  }
                           }
                     }
              } catch(Exception e) {
                     System.out.println("Exception in select Stament"+e.getMessage());
              }
       }
      
       public void deleteValues() throws Exception{
              try {
                     session.execute(deleteStmt.bind("Ramu"));
              } catch(Exception e) {
                     throw e;
              }
       }
      
       public void updateValues() throws Exception {
              try {
                     session.execute(updateStmt.bind(18L,"Ramu"));
              } catch(Exception e) {
                     throw e;
              }
       }
      
       public static void main(String args[]) throws Exception {
              try {
                     CassandraConnection cc = new CassandraConnection();
                     cc.insertValues();
                     cc.retriveValues();
                     cc.updateValues();
                     cc.deleteValues();
              } 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.



0 comments:

Post a Comment

 
Top