Alter a keyspace
Either the strategy class or replication factor of a keyspace can be modified.
To change a simple replication factor, either increase or decrease the value to change the total number of copies of keyspace data stored in a cluster. Changing the replication factor of a keyspace impacts each node that the keyspace replicates to (or no longer replicates to), and data will require time to redistribute.
The most common replication strategy modification is to switch the strategy class from SimpleStrategy
to NetworkTopologyStrategy (NTS)
.
When the NetworkTopologyStrategy
class is set, the replication_factor
must be modified to specify one or more datacenters.
Altering the distribution of nodes within multiple datacenters when data is present should be accomplished by adding a datacenter. Then add data to the new nodes in the new datacenter and remove nodes from the old datacenter.
Follow this procedure to prepare all affected nodes for this change.
You cannot insert data into a table in a keyspace that uses |
See Data replication for more information.
Prerequisites
-
A keyspace must exist.
-
A full repair must be run on the keyspace if you add datacenters or reduce the replication factor.
Change simple replication_factor
value
Use the ALTER KEYSPACE
CQL command to change the value of a keyspace using the SimpleStrategy
class:
ALTER KEYSPACE cycling
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 2,
};
Now, a datacenter must be named, and not just a replication factor.
Change the replication strategy
and the replication_factor
values
Update a keyspace in the cluster and change its replication strategy options to either increase or decrease the replication factor.
ALTER KEYSPACE cycling
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'DC1' : 3,
'DC2' : 2
};
Verify the case of the datacenter name before using this command, as datacenter names are case-sensitive.
Run full repair, if required
Run a full repair of the keyspace on all nodes belonging to the datacenters where the replication factor has been increased. Also, run a full repair of the keyspace in the datacenters that have been added to the replication list.
nodetool repair --full -pr <keyspace_name>
If you decrease the replication factor, run a cleanup of the keyspace on all nodes belonging to the datacenters that had their replication factor decreased. Also, run a cleanup of the keyspace on the datacenters that were removed from the replication list. This action will free up the space held by the now unused replicas of the data.
nodetool cleanup <keyspace_name>
See also: