• Articles
  • Tutorials
  • Interview Questions

Cassandra Client API

Cassandra Client API

There are multiple client api available for Cassandra Training. However the preferred interface to Cassandra 1.2 and 2.0 is CQL. Earlier it used to be thrift.
List of all available Cassandra clients can be found at
https://wiki.apache.org/cassandra/ClientOptions#CQL
Briefly we would talk about Java cql. Source code is self explanatory
package com.intellipaad;

import com.datastax.driver.core.BoundStatement;
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;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.QueryBuilder;
 
public class CassandraCliClientExample {
static void main(String[] args) {
// Connect to the cluster and keyspace "library"
Cluster cluster = Cluster.builder().addContactPoint("localhost")
.build();
Session session = cluster.connect("library");
/**
* *other ways to create cluster cluster =
* Cluster.builder().addContactPoint("192.168.0.30")
* .withRetryPolicy(DefaultRetryPolicy.INSTANCE) .build(); session =
* cluster.connect("library"); cluster =
* Cluster.builder().addContactPoint("192.168.0.30")
* .withRe.tryPolicy(DefaultRetryPolicy.INSTANCE)
* .withLoadBalancingPolicy( new TokenAwarePolicy(new
* DCAwareRoundRobinPolicy())) .build(); session =
* cluster.connect("library");
***/
 
// Insert one record into the customer table
session.execute("INSERT INTO customer (lastname, age, city, email, firstname)
VALUES ('Ram', 35, 'Delhi', '[email protected]', 'Shree')");
 
//Use select to get the customer we just entered
ResultSet results = session.execute("SELECT * FROM customer WHERE
lastname='Delhi'");
for (Row row : results) {
System.out.format("%s %dn", row.getString("firstname"), row.getInt("age"));
}
// Update the same customer with a new age
session.execute("update customer set age = 36 where lastname = 'Delhi'");
// Select and show the change
results = session.execute("select * from customer where lastname='Delhi'");
for (Row row : results) {
System.out.format("%s %dn", row.getString("firstname"), row.getInt("age"));
}
// Delete the customer from the customer table
session.execute("DELETE FROM customer WHERE lastname = 'Delhi'");
// Show that the customer is gone
result= session.execute("SELECT * FROM customer");
for (Row row : results) {
System.out.format("%s %d %s %s %sn", row.getString("lastname"),
row.getInt("age"), row.getString("city"),
row.getString("email"), row.getString("firstname"));
}
// Insert one record into the customer table
PreparedStatement statement = session.prepare("INSERT INTO customer"
+ "(lastname, age, city, email, firstname)"
+ "VALUES (?,?,?,?,?);");
BoundStatement boundStatement = new BoundStatement(statement);
session.execute(boundStatement.bind("Ram", 35, "Delhi",
"[email protected]", "Shree"));
// Use select to get the customer we just entered
Statement select = QueryBuilder.select().all().from("library", "customer")
.where(QueryBuilder.eq("lastname", "Ram"));
results = session.execute(select);
for (Row row : results) {
System.out.format("%s %d n", row.getString("firstname"),
row.getInt("age"));
}
// Delete the customer from the customer table
Statement delete = QueryBuilder.delete().from("customer")
.where(QueryBuilder.eq("lastname", "Ram"));
results = session.execute(delete);
// Show that the customer is gone
select = QueryBuilder.select().all().from("library", "customer");
results = session.execute(select);
for (Row row : results) {
System.out.format("%s %d %s %s %sn", row.getString("lastname"),
row.getInt("age"), row.getString("city"),
row.getString("email"), row.getString("firstname"));
}
// Clean up the connection by closing it
cluster.close();
}
}

Course Schedule

Name Date Details
Big Data Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Big Data Engineering

Abhijit is a Technical Research Analyst specialising in Big Data and Azure Data Engineering. He has 4+ years of experience in the Big data domain and provides consultancy services to several Fortune 500 companies. His expertise includes breaking down highly technical concepts into easy-to-understand content.