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

Cassandra deletes the index


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to delete the index

You can use the command DROP INDEX to delete the index. The syntax is as follows:

DROP INDEX <identifier>

An example of deleting an index of a column in a table is given below. Here we delete the index of the column names in table emp.

cqlsh:tp> drop index name;

Use the Java API to delete the index

You can use the Execute() method of the Session class to remove the index of the table. Follow these steps to remove the index from the table.

Step 1: Create a cluster object

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 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

Create an instance of a Session object using the Connect() method of the Cluster class, 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 do so by formatting the KeySpace name as an existing key space in string format, as shown below.

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

Here we use KeySpace to name tp. Therefore, the session object is created as follows.

Session session = cluster.connect(“ tp” );

Step 3: Execute the query

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

In the following example, we removed the index "name" of the emp table. You must store the query in a string variable and pass it to the execute() method, as shown below.

//Query
String query = "DROP INDEX user_name;";
session.execute(query);

The full program for removing indexes in Cassandra using the Java API is shown below.

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

public class Drop_Index {

   public static void main(String args[]){
     
      //Query
      String query = "DROP INDEX user_name;";
 
      //Creating cluster object
      Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();.
 
      //Creating Session object
      Session session = cluster.connect("tp"); 
 
      //Executing the query
      session.execute(query);
      
      System.out.println("Index dropped");
   }
}

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 Drop_index.java
$java Drop_index

Under normal conditions, it should produce the following output:

Index dropped