Modifying keyspace in Cassandra

Prerequisite – Cassandra
In this article, we will discuss how we can modify the existing keyspace by using the ALTER KEYSPACE command. It is very helpful when we want to modify some properties such that the replication factor, data centre name, keyspace name, etc.

Procedure:
First, we need to create keyspace. Let’s consider cluster1 is the keyspace and we have strategy options like NetworkTopologyStrategy and data centers name east and west in which replication factor is 2 for both the data centers. Let’s have a look.

Creating an keyspace:

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

If we are using SimpleStrategy then we can use the following CQL query to modifying the existing keyspace.

ALTER KEYSPACE cluster1
WITH REPLICATION =  { 'class' : 'SimpleStrategy', 
                      'replication_factor' : 3 }; 

Changing the Replication Strategy:
In case of modifying we can set the RF (replication factor) and replication strategy at keyspace level. let’s understand this with an example.



ALTER KEYSPACE cluster1
WITH REPLICATION = {'class’: 'NetworkTopologyStrategy', 
                    'east1' : 3, 'west1' : 3}; 

Now, if we want to modify an existing keyspace in which we have restriction to change keyspace name. let’s have a look.

ALTER KEYSPACE cluster1
WITH REPLICATION = {'class’: 'NetworkTopologyStrategy', 
                    'east1' : 3, 'dc2' : 2}; 

Note: Datacenter names are case-sensitive. Verify the case of the datacenter name using a utility, such as dsetool status.
See Changing keyspace replication strategy.

dsetool status

Now, we can run a full repair to the keyspace when we are going to add data centers. let’s have a look.

nodetool repair --full keyspace_name
nodetool repair –full cluster1; 

Now, to verify the keyspace changes used the following CQL Query.

describe keyspace cluster1; 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced 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.