Monday, April 15, 2013

Using secondary indexes in Cassandra CQL: need to use an indexed column in your query

Say, i create this table:

cqlsh:Keyspace2> CREATE TABLE users (   user_name varchar PRIMARY KEY,   password varchar,   gender varchar,   session_token varchar,   state varchar,   birth_year bigint );
cqlsh:Keyspace2> INSERT INTO users
             ...          (user_name, password)
             ...          VALUES ('jsmith', 'ch@ngem3a');
cqlsh:Keyspace2> INSERT INTO users          (user_name, password)          VALUES ('jsmith2', 'ch@ngem3a2');
cqlsh:Keyspace2> create index on users (password);
I can create an index and query:

cqlsh:Keyspace2> select * from users where password = 'ch@ngem3a2' and user_name = 'jsmith2';
 user_name | birth_year | gender | password   | session_token | state
-----------+------------+--------+------------+---------------+-------
   jsmith2 |       1963 |   null | ch@ngem3a2 |          null |  null

 but this doesnt work:
cqlsh:Keyspace2> select * from users where birth_year > 1960;
Bad Request: No indexed columns present in by-columns clause with Equal operator
Perhaps you meant to use CQL 2? Try using the -2 option when starting cqlsh.
cqlsh:Keyspace2> select * from users where birth_year > 1960 and user_name 'jsmith';
Bad Request: line 1:48 no viable alternative at input 'user_name'
cqlsh:Keyspace2> select * from users where birth_year > 1960 and user_name = 'jsmith';
Bad Request: No indexed columns present in by-columns clause with Equal operator
Perhaps you meant to use CQL 2? Try using the -2 option when starting cqlsh.

Unless i use an indexed column in the query (not necessarily the primary indexed one):

cqlsh:Keyspace2> select * from users where birth_year > 1960  and password = 'ch@ngem3a2';
 user_name | birth_year | gender | password   | session_token | state
-----------+------------+--------+------------+---------------+-------
   jsmith2 |       1963 |   null | ch@ngem3a2 |          null |  null 

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.