Create & Drop INDEX in Cassandra

Cassandra Create Index

Command 'Create index' creates an index on the column specified by the user. If the data already exists for the column you want to index, Cassandra creates indexes on the data during the 'create index' statement execution.

That's why, for filtering columns in Cassandra, indexes needs to be created.

Syntax

Create index IndexName on KeyspaceName.TableName(ColumnName);

Example

Here is the snapshot where it was tried to filter "dept" column without creating the index. In response, the error was returned.

Create & Drop INDEX in Cassandra: Complete Tutorial

Here is the snapshot where index is created on dept column.

Create & Drop INDEX in Cassandra: Complete Tutorial

Create index DeptIndex on University.Student(dept);

Here is the snapshot where it will be successfully filtered 'dept' column.

Create & Drop INDEX in Cassandra: Complete Tutorial

select * from University.Student where dept='CS';

Cassandra Drop Index

Command 'Drop index' drops the specified index. If index name was not given during index creation, then index name is TableName_ColumnName_idx.

Syntax

Drop index IF EXISTS KeyspaceName.IndexName 

Example

Here is the snapshot of the executed command 'Drop index' that drops the index DeptIndex.

Create & Drop INDEX in Cassandra: Complete Tutorial

drop index IF EXISTS University.DeptIndex;

After successful execution of the command, DeptIndex will be dropped from the keyspace. Now data cannot be filtered by the column dept.

 

YOU MIGHT LIKE: