Thursday, April 09, 2015

Cassandra - No secondary indexes on the restricted columns support the provided operators

If you see this error in a "cqlsh" query, probably you are try to query a column value which is not indexed. In cassanday, you can declare a secondary index on a column family. It is called secondary index in order to distiguish from the index on the row key that all column families have. Secondary indexes allow querying by value and can be built in the background automatically without blocking reads or writes. For example:

cqlsh:tweetsentiment> select * from tweets limit 1;
 key                                  | data                                                                                       | prediction | timestamp

--------------------------------------+--------------------------------------------------------------------------------------------+------------+------------

 02af3941-f21e-4302-8d25-fa989a37a858 | RT @WhiteVsBlackTwt: Obama better hope his daughter ain't got a kik http://t.co/VBQ7jCiPM2 |        4.0 | 1428615131

You can query on the row key, but if you want to query on timestamp, you will get error:

cqlsh:tweetsentiment> select count(*) from tweets where timestamp = '1428614831';

InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: "

To create a secondary index on timestamp:

cqlsh:tweetsentiment> create index on  tweets (timestamp);

cqlsh:tweetsentiment> select * from tweets where timestamp = '1428614831' limit 1;

 key                                  | data                                                                    | prediction | timestamp

--------------------------------------+-------------------------------------------------------------------------+------------+------------

 24cf8862-1391-4864-9cbd-4370526a08ac | RT @vaneedope: @quiinoo_ @ogbhay_ she said you should Kik her sometime  |        0.0 | 1428614831


To remove a secondary index:

 cqlsh:tweetsentiment> DROP index tweets_timestamp_idx;

DROP index table_column_idx

You can read more:
http://www.datastax.com/dev/blog/whats-new-cassandra-07-secondary-indexes

No comments: