Open In App

Create database in Cassandra

Improve
Improve
Like Article
Like
Save
Share
Report

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; 

Last Updated : 28 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads