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

Cassandra creates an index

May 17, 2021 Cassandra

Table of contents

Use Cqlsh to create an index

You can use the command CREATE INDEX to create an index in Cassandra. The syntax is as follows:

CREATE INDEX <identifier> ON <tablename>

Here is an example of an index of a column.Here, we created an index for column "EMP_NAME" in a table named EMP.

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

Create an index with Java API

You can use the SESSION class's Execute () method to create the index of the column column.Follow the steps given below to create an index for columns in the table.

first step: Create a cluster object

First, create a named com.datastax.driver.core of Cluster.builder The instance of the class is as shown below.

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

use Cluster.Builder Objective addContactPoint() Method Add a contact point (Node's IP address).This method returns Cluster.Builder

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

Create a cluster object with a new builder object.To this end, Cluster.Builder There is a name in the class build() Methods.The following code shows how to create a cluster object.

//Building a cluster
Cluster cluster =;

You can build cluster objects using a single line code as shown below.

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

Step 2: Create a session object

use Cluster The CONNECT () method of the class creates 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 the keySpace name to the existing key space by setting the Keyspace name to the existing key space, this method is shown below.

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

Here we use Keyspace called TP.Therefore, create a session object as follows.

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

Step 3: Execute Query

You can perform CQL queries using the Execute () method of the Session class.Pass the query to the Execute () method with a string format or a STATEMENT class object.Whether you pass in a string format to this method cqlsh Decrease.

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

String query = "CREATE INDEX name ON emp1 (emp_name);";

The complete program for creating columns indexes in tables in Cassandra using the Java API is shown below.

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

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

      String query = "CREATE INDEX name ON emp1 (emp_name);";
      Cluster cluster = Cluster.builder().addContactPoint("").build();
      //Creating Session object
      Session session = cluster.connect("tp");
      //Executing the query
      System.out.println("Index created");

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.

$java Create_Index

Under normal conditions, it should produce the following output:

Index created