Sunday 12 January 2020

Cassandra: Create keyspace if it is not already exist


If you are trying to create the keyspace, that is already created, then you will get 'AlreadyExists: Keyspace 'cassandratutorial' already exists' error

cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = { 
   ... 'class' : 'NetworkTopologyStrategy', 
   ... 'bangalore' : 3, //Datacenter 1
   ... 'tokyo' : 2, //Datacenter 2
   ... 'berlin' : 3 //Datacenter 3 
   ...  };
cqlsh> 
cqlsh> DESCRIBE KEYSPACE cassandratutorial;

CREATE KEYSPACE cassandratutorial WITH replication = {'class': 'NetworkTopologyStrategy', 'bangalore': '3', 'berlin': '3', 'tokyo': '2'}  AND durable_writes = true;

cqlsh> 
cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = { 
   ... 'class' : 'NetworkTopologyStrategy', 
   ... 'bangalore' : 3, //Datacenter 1
   ... 'tokyo' : 2, //Datacenter 2
   ... 'berlin' : 3 //Datacenter 3 
   ...  };
AlreadyExists: Keyspace 'cassandratutorial' already exists

To get rid of these kind of problems, create a keyspace, only if it is not exist (CREATE KEYSPACE IF NOT EXISTS).

CREATE KEYSPACE IF NOT EXISTS cassandratutorial WITH REPLICATION = {
         'class' : 'NetworkTopologyStrategy',
         'bangalore' : 3, //Datacenter 1
         'tokyo' : 2, //Datacenter 2
         'berlin' : 3 //Datacenter 3
 };


Above statement creates a keyspace 'cassandratutorial' if it is not exists, else it ignore the above command.

Previous                                                    Next                                                    Home

No comments:

Post a Comment