Basics of Cassandra (with Java connectivity)

Apache Cassandra is a free distributed, high performance, scalable fault tolerant post-relational DB system (which follows flat file DB system).

Typically this is used when huge data needs to be stored (in flat-file system) in a no-sql environment. The DB was primarily used by Facebook and later it was popular as a no-sql, flat file DB system. This DB is used extensively for business intelligent systems.

Advantage of Cassandra over SQL DB are:

No fixed schema (as no-sql), freq of handling the data is very high and a huge volume of data can be handled easily using Cassandra. Moreover, it doesnot have a single point of failure and Cassandra is deployed in Horizontal fashion.

Now, you might be wondering why Cassandra is fault-tolerant (or why there is no single point of failure)?

The answer lies in the architecture of Cassandra. It has a master-less design with the concept of Node and all the nodes are treated as same in Cassandra.


What is the advantage that Cassandra is providing with respect to other no-sql DBs. In short, why Cassandra?

Simple reason is, the fault tolerant structure Cassandra provides that can be replicated in any Data center without hiccups (irrespective of  geographical boundaries). Moreover it supports 50,000 transactions per second per node. Which makes the transaction super fast.



Components of Cassandra:

Node (place where Data is stored).

Data Center (collection of Nodes).

Cluster (collection of Data Centers).

Commit log: Mainly used for crash recovery mechanism. As all the transaction is written in commit log.

Mem-table: After the data is written in commit-log then it is stored in mem-table.

SS table: This is a disk file, while data is flushed (when data reached its threshold value) from mem-table, it is stored in SS table.


What you need: JDK, downloaded exe file from DataStax , Cassandra CQL shell (where we can write cqlsh, which is nothing but cassandra query language shell).

We can use commands like: Help, Copy, Capture, Describe, Exit, Create Keyspace, create table, alter table, drop table, insert, update, delete, alter, drop, select, batch etc.


The driver should contain: driver-core, driver-mapping, driver-extras, driver-tests (use maven to get the latest versions)

Connecting to Cassandra DB using Java:
We need a cluster object. Address of a node needs to be provided else default address 9042 will be used. Sample program to connect to DB:
public class ConnectorForCassandra {
private Cluster cluster;
private Session session;
public void connect(String node, Integer port) {
 Builder b = Cluster.builder().addContactPoint(node);
 if (port != null) {
 cluster =;
 session = cluster.connect();
public Session getSession() {
 return this.session;
public void close() {
After connecting to DB we need to create a keyspace (which is similar to a schema of a SQL DB).
KeyspaceRepository class is given in the jar to create a keyspace like below:
new KeyspaceRepository().createKeyspace(keyspaceName, "SimpleStrategy", 1);
Once the keyspace is created we can create our own DB and then we can run query based on requirements.
Digiprove sealCopyright secured by Digiprove © 2019 Geeks 18


Leave a Reply

Your email address will not be published.