Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra modifies the key space


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to modify Keyspace

ALTER KEYSPACE can be used to change properties, such as the number of repricas and durable_writes KeySpace. The syntax of this command is given below.

Statement

ALTER KEYSPACE <identifier> WITH <properties>

That is:

ALTER KEYSPACE “KeySpace Name”
WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of  replicas’};

ALTER KEYSPACE has the same properties as CREATE KEYSPACE. It has two properties: replication and durable_writes.

Durable_writes

With this option, you can indicate whether Cassandra uses cometlog for the current KeySpace update. This option is not mandatory and is set to true by default.

Example

Here's an example of modifying KeySpace.

  • Here we change a KeySpace called TutorialsPoint.

  • We changed the replication factor from 1 to 3.

cqlsh.> ALTER KEYSPACE tutorialspoint
WITH replication = {'class':'NetworkTopologyStrategy', 'replication_factor' : 3};

Change Durable_writes

You can also modify KeySpace'durable_writes properties. Here are some of the properties durable_writes KeySpace.

SELECT * FROM system.schema_keyspaces;

  keyspace_name | durable_writes |                                       strategy_class | strategy_options
----------------+----------------+------------------------------------------------------+----------------------------
           test |          False | org.apache.cassandra.locator.NetworkTopologyStrategy | {"datacenter1":"3"}

 tutorialspoint |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"4"}

         system |           True |           org.apache.cassandra.locator.LocalStrategy | { }

  system_traces |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"2"}
(4 rows)

ALTER KEYSPACE test
WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3}
AND DURABLE_WRITES = true;

Again, if you verify keySpaces' properties, it will produce the following output.

SELECT * FROM system.schema_keyspaces;
  keyspace_name | durable_writes |                                       strategy_class | strategy_options
----------------+----------------+------------------------------------------------------+----------------------------
           test |           True | org.apache.cassandra.locator.NetworkTopologyStrategy | {"datacenter1":"3"}

 tutorialspoint |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"4"}

         system |           True |           org.apache.cassandra.locator.LocalStrategy | { }

  system_traces |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"2"}

(4 rows)

Use the Java API to modify the key space

You can use the Easycute() method of the Session class to change the key space. Follow these steps to change the key space using the Java API

Step 1: Create a cluster object

First, create an instance of the Cluster.builder class called com.datastax.driver.core, as shown below.

//Creating Cluster.Builder object
Cluster.Builder builder1 = Cluster.builder();

Use the addContactPoint() method of the Cluster.Builder object to add a contact point (the IP address of the node). This method returns to Cluster.Builder.

//Adding contact point to the Cluster.Builder object
Cluster.Builder builder2 = build.addContactPoint( "127.0.0.1" );

Create a cluster object with the new builder object. T o do this, there is a method called build () in the Cluster.Builder class. The following code shows how to create cluster objects.

//Building a cluster

Cluster cluster = builder.build();

You can use a single line of code to build cluster objects, as shown below.

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

Step 2: Create a session object

Use Cluster class's connect() method to create an instance of a Session object, as shown below.

Session session = cluster.connect( );

This method creates a new session and initializes it. If you already have a key space, you can set it to the existing key space by passing the keyspace name in string format to this method, as shown below.

Session session = cluster.connect(“ Your keyspace name ” );

Step 3: Execute the query

You can use the Execute() method of the Session class to perform CQL queries. P ass the query in string format or statement class objects to the execute() method. Whether you pass this method in string format will execute on cqlsh.

In this example,

  • We are changing a key space called tp. We are changing the replication option from a simple policy to a network topology policy.

  • We are durable_writes change the value to false

You must store the query in a string variable and pass it to the execute() method, as shown below.

//Query
String query = "ALTER KEYSPACE tp WITH replication " + "=   {'class':'NetworkTopologyStrategy', 'datacenter1':3}" +" AND DURABLE_WRITES = false;";
session.execute(query);

Here's a complete program for creating and using key spaces in Cassandra using the Java API.

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class Alter_KeySpace {
   public static void main(String args[]){

      //Query
      String query = "ALTER KEYSPACE tp WITH replication " + "= {'class':'NetworkTopologyStrategy', 'datacenter1':3}"
         + "AND DURABLE_WRITES = false;";

      //Creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
   
      //Creating Session object
      Session session = cluster.connect();
 
      //Executing the query
      session.execute(query);
 
      System.out.println("Keyspace altered");
   }
}

Use the class name and .java save the above program and browse to the save location. Compile and execute the program as shown in the following image.

$javac Alter_KeySpace.java
$java Alter_KeySpace

Under normal conditions, it produces the following output:

Keyspace Altered