Create database in Cassandra

In this article, we are going to discuss how to create the database in Cassandra. so, for better understanding you can read “Introduction to Cassandra” article before creating a database in Cassandra.

Step-1: login into cqlsh
you can log in into the cqlsh using Cassandra default credential. Now, here before creating a database you first need login into cqlsh. you can check the cluster information after login into cqlsh.let’s have a look.

cqlsh 127.0.0.1 -u cassandra -p cassandra 

Step-2: Creating a database
Creating a keyspace in Cassandra is the same as creating a database in SQL. CQL query for creating a keyspace as following.

Syntax: 

CREATE  KEYSPACE [IF NOT EXISTS] keyspace_name                
   WITH REPLICATION = {replication_map}
   [AND DURABLE_WRITES =  true|false] ; 

Here’s an example that shows how to create a keyspace named App_data:



you must read replication strategy in Cassandra for better understanding.

Replication Strategy : NetworkTopologyStrategy
cqlsh> CREATE KEYSPACE IF NOT EXISTS App_data
       WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy',
       'datacenter1' : 3, 'datacenter2': 2 }; 

Now, here you must check If there are no errors, the database is created, or not then You can run the describe keyspaces command to ascertain that the database did indeed create the keyspace.

To check the all keyspaces which are already created then you can run the following CQL query given below.

cqlsh> describe keyspaces; 

To check the App_data keyspaces is created or not then you can run the following CQL query given below.

cqlsh> describe App_data; 

Output:

cassandra@cqlsh> describe keyspaces;

university   system    backup_copy     system_traces  system_schema
system_auth  app_data  system_distributed  operation 

In Cassandra, IF NOT EXISTS part is optional but it is always good practice using such statements because it helps avoid an error should the keyspace cycling already exist.

Step-3: Describing the keyspace
In Cassandra, A keyspace is a defining container for replication. Now, You can see the new keyspace App_data in the list of keyspaces shown by the database.

Now, to check the details about App_data keyspace, you can execute the describe App_data command.

describe App_data; 

Output:

cassandra@cqlsh> describe App_data;

CREATE KEYSPACE app_data WITH 
replication = {'class': 'NetworkTopologyStrategy', 
               'datacenter1': '3', 'datacenter2': '2'}  
AND durable_writes = true; 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.