Sunday 19 January 2020

Cassandra: Alter keyspace: Change keyspace replication strategy

You can change the replication strategy of the keyspace using ‘ALTER KEYSPACE’ command.

Syntax
ALTER  KEYSPACE keyspace_name 
   WITH REPLICATION = { 
      'class' : 'SimpleStrategy', 'replication_factor' : N  
     | 'class' : 'NetworkTopologyStrategy', 'dc1_name' : N [, ...] 
   }
   [AND DURABLE_WRITES =  true|false] ;

For example, let me create a key space ‘cassandratutorial’ with 'SimpleStrategy'.
CREATE keyspace cassandratutorial WITH REPLICATION =
{
         'class' : 'SimpleStrategy',
         'replication_factor' : 3
};

cqlsh> CREATE keyspace cassandratutorial WITH REPLICATION = 
   ... { 
   ... 'class' : 'SimpleStrategy', 
   ... 'replication_factor' : 3 
   ... };
cqlsh> 
cqlsh> DESCRIBE cassandratutorial;

CREATE KEYSPACE cassandratutorial WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

Now, you can change the replication strategy of the keyspace ‘cassandratutorial’ by executing below command.

ALTER KEYSPACE cassandratutorial WITH REPLICATION = {
         'class' : 'NetworkTopologyStrategy',
         'bangalore' : 3, //Datacenter 1
         'tokyo' : 2, //Datacenter 2
         'berlin' : 3 //Datacenter 3

 };

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

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

Note

You should run the command 'nodetool repair -full keyspace', once replication strategy is changed.


Previous                                                    Next                                                    Home

No comments:

Post a Comment