Cassandra Create & Drop Index
In this Tutorial you will learn about the two parts of Cassandra Create & Drop index in detail likewise;
- How to create index in Cassandra: step by step procedure?
- How to drop an index in Cassandra: step by step procedure?
Cassandra Create Index
To create index in Cassandra tables on the column specified by user we use Command ‘Create index’. While executing the command create index Cassandra create index on column if the data is already present in the column.
- Once the index is created on column Cassandra indexes new inserted data automatically.
- As primary is key is already indexed in cassandra so index cannot be created on primary key.
- Cassandra not support Indexes on entire collections.
- If you want to filter on some column in Cassandra query. It is necessary to create index on that column. Because Cassandra can’t filter not indexed columns unless it is a primary key. As primary key is by default indexed.
Syntax for create index in cassandra
Create index IndexName on KeyspaceName.TableName(ColumnName);
Example to create index
Here is the screenshot that depict the scenario when try to filter the column whose indexed is not created. The following error code arises in response to query when i try to filter degree column without creating index on it.
Here is the screenshot of where index is created on degree column with name DegreeIndex.
Create Index on Degree Column Syntax.
Create index DegreeIndex on University.Student(degree);
The below screenshot show how the Cassandra behave for a query after creating index on column.
Syntax
select * from University.Student where degree='MSCS';
Cassandra Drop Index
To drop an index in Cassandra the Command ‘Drop index’ is used for a specified index along with the name of index name TableName_ColumnName_idx.
- The command return an error if the specified index does not exist, until or unless IF EXISTS parameter is used that will return no error.
- While you are creating an index, you have to specify keyspace name along with the index name. Otherwise Cassandra will drop index from the current keyspace.
Syntax to Drop Index in cassandra
Drop index IF EXISTS KeyspaceName.IndexName
Example
Here is the screenshot that depict the complete procedure to drop index in Cassandra cluster. we have executed command ‘Drop index’ that drops the index DegreeIndex.
Syntax to Drop Index
drop index IF EXISTS University.DegreeIndex;
After successful execution of the command ‘Drop Index’. DegreeIndex will be dropped from the Keyspace University. Now you are unable to filter data by the column dept.
Here is the screenshot that depict the scenario when try to filter the column whose indexed is dropped. The same error code returned by Cassandra in response to query. When i try to filter degree column without creating index on that column.
Summary:
This tutorial explain the complete step by step procedure for cassandra Create index as well Cassandra Drop Index in Cassandra Cluster.