1

Using version 3 of the Cassandra Java driver, how do you determine whether a column has a secondary index on it?

Using version 2 of the driver, I could simply check whether ColumnMetadata.getIndex() returns non-null. But that method was removed:

This is due to the fact that secondary indexes have been completely redesigned in Cassandra 3.0... Unfortunately, there is no easy way to recover the functionality provided by the deleted method

Raedwald
  • 46,613
  • 43
  • 151
  • 237

1 Answers1

2

The authors of the Cassandra driver recognize that this is a breaking change. It is impossible to determine whether a column has an index on it using the available meta-data, because the index implementation can delegate to an arbitrary Java class: secondary indexes are no longer the only kind of index.

You can work-around this by using a consistent naming scheme for secondary indexes, or using the default naming scheme for secondary indexes, then querying the table meta-data of the table of the column to see whether it has any index with the expected name. The default naming scheme is <table_name>_<column_name>_idx.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Alternatively, you could iterate of all indexes of a given table, and for each of them, inspect their target attribute (https://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/IndexMetadata.html#getTarget--). The target attribute is the "raw" fragment of CQL that comes right after the `CREATE INDEX ON` clause. For simple secondary indexes, it should contain the name of the indexed column. – adutra May 17 '16 at 14:59
  • 1
    @adutra I considered that originally, but I could not find any documentation that described the `target` attribute, so I concluded that using the `name` would be safest. – Raedwald May 17 '16 at 16:03