Create Keyspace in Cassandra

Cassandra Create Keyspace
To communicate with Cassandra the Cassandra Query Language (CQL) is used. A keyspace is an object working similar to an RDBMS database. It holds column families, the strategy used in the keyspace, indexes, user-defined types, replication factor, data centre awareness, etc.

Syntax 1: To create keyspace:

CREATE KEYSPACE <identifier> WITH <properties>

Syntax 2: To create keyspace:

CREATE KEYSPACE Keyspace_Name with replication={'class':strategy name,
'replication_factor': No of replications on different nodes}

 

Components of Cassandra Keyspace:

Strategy:
In Cassandra syntax, there are two types of strategy declaration.

Simple Strategy:
In the case of one data centre, this strategy is preferred. The first replica is thus placed on the selected node in the process. In this strategy, the remaining nodes are placed in the ring in the clockwise direction. The rack or node location is not considered.

Network Topology Strategy:
In the case of more than one data centres, the Network Topology strategy is used. A replication factor is provided in this strategy for each data centre separately.

Replication Factor:
Replication factor is the number of the machine in the cluster which will be used in replication, i.e, the number of machines receiving the copies of the same data.

Example:

CREATE KEYSPACE example
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

Explanation:
A keyspace named ‘example’ is created in the above example.

Verification:
Use the “DESCRIBE” command for the verification of the keyspace creation.

Syntax:

DESCRIBE keyspaces;

 

Durable_writes:
The durable_writes is a property which is used with the CREATE KEYSPACE statement. By default, it is set to true. It can be set to false. But, cannot be set to simplex strategy.

Example:

CREATE KEYSPACE durable_example
WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 }
AND DURABLE_WRITES = false;

Verification:
Use the “DESCRIBE” command for the verification of the keyspace creation.

Syntax:

DESCRIBE keyspaces;

 

To use a Keyspace:

Syntax:

USE <identifier>

Example:

USE durable_example;