Create Keyspace in CQL

Category: Cassandra   Tags: Cassandra, Learning, Beginners, Basics, NoSQL Database, Cassandra CQL

Use CREATE KEYSPACE command to create keyspace in Cassandra using CQL:

CREATE  KEYSPACE | SCHEMA  IF NOT EXISTS keyspace_name
WITH REPLICATION = map
AND DURABLE_WRITES =  true | false
                        

map can have have below properties and values:

{ 'class' : 'NetworkTopologyStrategy', '<replication_factor>' : <integer>[
,'<data center>' : <integer> ,'<data center>' : <integer>] . . .
};
                        

class is the name of the replica placement strategy class for the new keyspace. It hastwo values 'SimpleStrategy' or 'NetworkTopologyStrategy'.

replication_factor The number of replicas of data on multiple nodes. Required if class is SimpleStrategy.

data center is the number of replicas of data on each node in the data center. Required if class is NetworkTopologyStrategy.

If a keyspace already exist then the create command will return error. If IF NOT EXISTS option is used then using the create statement will create the keyspace if it doesn't exist or will simply do nothing. Here is one example of creating keyspace:

CREATE KEYSPACE DemoKeyspace
  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
                        

NetworkTopologyStrategy is recommended for production. Check that the keyspaces were created:

SELECT * FROM system.schema_keyspaces;