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.
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.
This amazing article i have ever read in recent times. This is very inforamtive article. I regularly visit this blog for this kind fo helpful posts. Thank you so much for this wonderful blog post, keep posting such helpful information. If you are genuinely searching for a job oriented pega online training or pega online training hyderabad who are expertise to teach 100% practicals based course. And they provide certification material at pega training institutes in hyderabad and you can see this pega online training hyderabad.Once again thanks a lot for this wonderful blog article, your efforts are priceless.
ReplyDeleteIt's very useful blog post with inforamtive and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.
ReplyDeleteUsually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man learn pega Online Training
ReplyDelete