Cassandra - Create Index



Creating an Index using Cqlsh

You can create an index in Cassandra using the command CREATE INDEX. Its syntax is as follows −

CREATE INDEX <identifier> ON <tablename>

Given below is an example to create an index to a column. Here we are creating an index to a column ‘emp_name’ in a table named emp.

cqlsh:tutorialspoint> CREATE INDEX name ON emp1 (emp_name);

Creating an Index using Java API

You can create an index to a column of a table using the execute() method of Session class. Follow the steps given below to create an index to a column in a table.

Step1: Create a Cluster Object

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

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

Add a contact point (IP address of the node) using the addContactPoint() method of Cluster.Builder object. This method returns Cluster.Builder.

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

Using the new builder object, create a cluster object. To do so, you have a method called build() in the Cluster.Builder class. The following code shows how to create a cluster object.

//Building a cluster
Cluster cluster = builder.build();

You can build the cluster object using a single line of code as shown below.

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

Step 2: Create a Session Object

Create an instance of Session object using the connect() method of Cluster class as shown below.

Session session = cluster.connect( );

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

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

Here we are using the KeySpace called tp. Therefore, create the session object as shown below.

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

Step 3: Execute Query

You can execute CQL queries using the execute() method of Session class. Pass the query either in string format or as a Statement class object to the execute() method. Whatever you pass to this method in string format will be executed on the cqlsh.

In the following example, we are creating an index to a column called emp_name, in a table named emp. You have to store the query in a string variable and pass it to the execute() method as shown below.

//Query
String query = "CREATE INDEX name ON emp1 (emp_name);";
session.execute(query);

Given below is the complete program to create an index of a column in a table in Cassandra using Java API.

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

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

      //Query
      String query = "CREATE INDEX name ON emp1 (emp_name);";
      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 created");
   }
}

Save the above program with the class name followed by .java, browse to the location where it is saved. Compile and execute the program as shown below.

$javac Create_Index.java
$java Create_Index

Under normal conditions, it should produce the following output −

Index created
Advertisements