Apache Cassandra

Cassandra Alter Keyspace

“The ALTER KEYSPACE command allows you to modify various properties of an existing Cassandra keyspace. For example, you can use this command to change the replication strategy and number of replicas and enable or disable durable writes.”

In this post, we will walk you through how to use the ALTER KEYSPACE command to modify various properties of a Cassandra keyspace.

Let’s dive in.

Command Syntax

The ALTER KEYSPACE command follows a simple syntax as shown in the code snippet below:

Let’s take practical examples to illustrate how to use this command.

Create Sample Keyspaces

Before illustrating how to use the ALTER KEYSPACE command, let us set up sample keyspaces for illustration.

In this example, we will create two types of keyspaces. The first will use the SimpleStrategy replication strategy, and the second will use NetworkTopologyStrategy.

SimpleStrategy Keyspace

We can create a keyspace using the SimpleStrategy as shown in the code below:

cassandra@cqlsh> CREATE KEYSPACE simple
... WITH REPLICATION = {
... 'class': 'SimpleStrategy',
... 'replication_factor': 1
... };

The statement above creates a keyspace with the name “simple” using the SimpleStrategy replication method and a replication factor of 1.

NetworkTopologyStrategy Keyspace

The example below creates a keyspace using the Network Topology Strategy.

cassandra@cqlsh> CREATE KEYSPACE production
... WITH REPLICATION = {
... 'class': 'NetworkTopologyStrategy',
... 'datacenter1': 1
... };

The query above creates a keyspace called production using the Network Topology Strategy.

Alter Keyspace – Change Replication Factor

To illustrate how to update the keyspace’s replication factor, we can use the keyspace “simple” we created earlier.

The command syntax is as shown:

ALTER KEYSPACE <keyspace_name>
WITH replication = {'class': ‘SimpleStrategy, 'replication_factor' : <value>};

For example:

cassandra@cqlsh> ALTER KEYSPACE simple
... WITH REPLICATION = {
... 'class': 'SimpleStrategy',
... 'replication_factor': 2
... };

In this example, we increase the number of replicas from 1 to 2.

Keep in mind that increasing the number of replicas may require you to perform a full repair to redistribute the data.

Alter Keyspace – Change Replication Strategy

We can also alter the keyspace to change the replication strategy. For example, we can change the “simple” keyspace strategy to NetworkTopologyStrategy.

The command syntax is as shown:

ALTER KEYSPACE <keyspace_name>
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter_name' : N };

Example

cassandra@cqlsh> ALTER KEYSPACE simple
... WITH REPLICATION = {
... 'class' : 'NetworkTopologyStrategy',
... 'datacenter1': 3};

In this case, we change the replication strategy of the “simple” keyspace to NetworkTopologyStrategy.

Alter Keyspace – Enable or Disable Durable Writes

We can also enable or disable durable writes for an existing keyspace using the ALTER KEYSPACE command.

The command syntax is as shown:

ALTER KEYSPACE <keyspace_name>
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter_name' : 3N}
AND DURABLE_WRITES = false/true ;

For example, to allow bypass the commit log for the “production” keyspace, we can run:

cassandra@cqlsh> ALTER KEYSPACE production
... WITH REPLICATION = {
... 'class' : 'NetworkTopologyStrategy',
... 'datacenter1': 3}
... AND DURABLE_WRITES = false;

The command above alters the “production” keyspace and disables durable writes.

Conclusion

In this article, you learned how to use the ALTER KEYSPACE command to alter various properties of an existing keyspace in a Cassandra cluster.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list