Create Keyspace in Cassandra: Create, Alter & Drop with Example

What is Keyspace in Cassandra?

A Keyspace in Cassandra is a data container, similar to a database in RDMBS (Relational Database Management Systems). Cassandra Keyspace determines how data replicates on nodes.

How to Create Keyspace in Cassandra

A keyspace is an object that holds the column families, user defined types. In Cassandra, Keyspace is similar to RDBMS Database. Keyspace holds column families, indexes, user defined types, data center awareness, strategy used in keyspace, replication factor, etc.

Command “Create Keyspace” is used to create keyspace in Cassandra.

Syntax

Create keyspace KeyspaceName with replication={'class':strategy name, 
		'replication_factor': No of replications on different nodes};

Various Components of Cassandra Keyspace

  • Strategy: While declaring strategy name in Cassandra. There are two kinds of strategies declared in Cassandra Syntax.
  1. Simple Strategy: Simple strategy is used when you have just one data center. In this strategy, the first replica is placed on the node selected by the partitioner. Remaining nodes are placed in the clockwise direction in the ring without considering rack or node location.
  2. Network Topology Strategy: Network topology strategy is used when you have more than one data centers. In this strategy, you have to provide replication factor for each data center separately. Network topology strategy places replicas in nodes in the clockwise direction in the same data center. This strategy attempts to place replicas in different racks.
  • Replication Factor: Replication factor is the number of replicas of data placed on different nodes. For no failure, 3 is good replication factor. More than two replication factor ensures no single point of failure. Sometimes, the server can be down, or network problem can occur, then other replicas provide service with no failure.
  • Example: Here is the snapshot of the executed command “Create Keyspace” that will create keyspace in Cassandra.

Create Keyspace in Cassandra

Create keyspace University with replication={'class':SimpleStrategy,'replication_factor': 3};

After successful execution of command “Create Keyspace”, Keyspace University will be created in Cassandra with strategy “SimpleStrategy” and replication factor 3.

Alter Keyspace Cassandra

Command “Alter Keyspace” alters the replication factor, strategy name and durable writes properties in created keyspace in Cassandra.

Syntax

Alter Keyspace KeyspaceName with replication={'class':'StrategyName', 
	'replication_factor': no of replications on different nodes} 
    	with DURABLE_WRITES=true/false

Key aspects while altering Keyspace in Cassandra

  • Keyspace Name: Keyspace name cannot be altered in Cassandra.
  • Strategy Name: Strategy name can be altered by specifying new strategy name.
  • Replication Factor: Replication factor can be altered by specifying new replication factor.
  • DURABLE_WRITES :DURABLE_WRITES value can be altered by specifying its value true/false. By default, it is true. If set to false, no updates will be written to the commit log and vice versa.
  • Execution: Here is the snapshot of the executed command “Alter Keyspace” that alters the keyspace strategy from ‘SimpleStrategy’ to ‘NetworkTopologyStrategy’ and replication factor from 3 to 1 for DataCenter1.

Alter Keyspace Cassandra

Alter Keyspace University with replication={'class':'NetworktopologyStrategy', 'DataCenter1':1};

After successful execution of the command “Alter Keyspace”, Strategyname will be changed from ‘SimpleStrategy’ to ‘NetworkTopologyStrategy’ and replication factor will be changed from 3 to 1 for ‘DataCenter1.’

Cassandra Drop Keyspace

Command ‘Drop Keyspace‘ drops keyspace including all the data, column families, user defined types and indexes from Cassandra. Before dropping the keyspace, Cassandra takes a snapshot of the keyspace. If keyspace does not exist in the Cassandra, Cassandra will return an error unless IF EXISTS is used.

Syntax

Drop keyspace KeyspaceName

Example

Here is the snapshot of the executed command ‘Drop Keyspace’ that will drop keyspace University.

Cassandra Drop Keyspace

Drop keyspace University;

After successful execution of the command ‘Drop keyspace University’, keyspace University will be dropped from Cassandra with all the data and schema.

Here is the snapshot where the error is returned when tried to access keyspace that does not exist.

Cassandra Drop Keyspace

Note: There is no difference in drop keyspace and delete keyspace. Drop keyspace is equal to delete keyspace.