Create and Update a Keyspace in Cassandra

In Cassandra a keyspace can be considered as a scheme in Relational Database. Keyspaces are basically the containers for application data; they holds a group of similar column-families(tables in rdbms) all together. Generally a cluster has one keyspace per application and all the application data goes in it.

Application data is grouped into Keyspaces, so that a particular type of column-family replication can be applied to a set of data, therefore the data with different replication requirements should reside in different keyspaces.

1) Show all the keyspaces in Cassandra

To show all available or already created keyspaces, type following CQL script on Cassandra Shell:

cqlsh> describe keyspaces;

You will see a list of all Cassandra keyspaces something like this:

cqlsh> describe keyspaces; companydata system OpsCenter system_traces

2) Defining or Creating a keyspace in CQL(Cassandra Query Language)

CQL has a similar syntax as in SQL, but defining a keyspace is totally different as of defining a schema in relational database. In simplest way a keyspace definition contains two general terms 1) Replication Strategy and 2) Replication Factor. We will see more about them in few minutes. Lets create a simple keyspace with ?SimpleReplicationStrategy? and replication factor = 2;

CREATE KEYSPACE tbNext WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

Where tbNext is the keyspace name, to verify run the describe command again you will see a new keyspace with name ?tbNext? added to the list.

cqlsh> describe keyspaces; companydata system OpsCenter system_traces tbNext

3) Use a keyspace

To use a keyspace type a sql like syntax, like:

cqlsh> use tbNext

You will see, the changed keyspace screen like:

cqlsh> use tbNext; cqlsh:tbNext>

4) Show all the tables in a cassandra keyspace

All the tables in a cassandra keyspace can be listed using, following command:

cqlsh:tbNext> describe tables;

We saw a brief about CQL usage, and how to create a keyspace in Cassandra, how to use a keyspace, how to show all keyspaces and how to show all the tables or column-families in Cassandra.

What is Replication Factor in Cassandra?

Replication is the process of storing copies of data on multiple nodes to ensure reliability and fault tolerance. Cassandra stores replicas of Data in different nodes of a cluster with the help of row keys. Replication Factor is the number of copies per row throughout the Cluster. If the replication factor is 1, that means there is a single copy of each row on a single node in the cluster. If replication factor is 2 that means there are two different copies of each row in two different nodes of the cluster, that?s why replication factor should not exceed the number of nodes in the cluster. If replication factor is greater than the number of nodes in the cluster, than writes can be rejected but reads can be fulfilled.

What is Replication Strategy in Cassandra?

Replication Strategy is other useful term to be passed at the time of keyspace creation, there are two replication strategies.

1) Simple Strategy
2) Networked Topology Strategy

Simple Strategy is used when data is stored on a single Data Centre cluster, in this strategy the very first replica is placed on the node determined by partitioner. Other replicas are place to the next nodes in clock wise direction.

Networked Topology Strategy is used when data is stored in multiple data centres, this strategy is helpful in specifying how many replicas to put in each data centre.

How to update replication factor in Cassandra

In real time application, you may need to change replication factor or replication strategy at some point of time. Initially if you have replication factor as 1, meaning that every row has a single copy on a node of cluster. This is a bit risky if that single node goes down, everything will stop working; so its better to have replication factor at least 2.

Here is simple syntax to change replication variables of Cassandra keyspaces.

1) Changing SimpleStrategy to NetworkTopologyStrategy

ALTER KEYSPACE tbNext WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2};

2) Changing NetworkTopologyStrategy to SimpleStrategy

ALTER KEYSPACE tbNext WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };

This is all about creating and updating a Cassandra keyspace using cql, in upcoming articles we will see more about Cassandra and Related No Sql Technologies.