Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cassandra, select via a non primary key

I'm new with cassandra and I met a problem. I created a keyspace demodb and a table users. This table got 3 columns: id (int and primary key), firstname (varchar), name (varchar). this request send me the good result:

SELECT * FROM demodb.users WHERE id = 3;

but this one:

SELECT * FROM demodb.users WHERE firstname = 'francois';

doesn't work and I get the following error message:

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

This request also doesn't work:

SELECT * FROM users WHERE firstname  = 'francois'  ORDER BY id DESC LIMIT 5;
InvalidRequest: code=2200 [Invalid query] message="ORDER BY with 2ndary indexes is not supported."

Thanks in advance.

like image 783
mel Avatar asked Apr 22 '15 09:04

mel


People also ask

Is primary key mandatory in Cassandra?

The primary key is a column that is used to uniquely identify a row. Therefore,defining a primary key is mandatory while creating a table.

How do I select data in Cassandra?

Cassandra provides standard built-in functions that return aggregate values to SELECT statements. A SELECT expression using COUNT(column_name) returns the number of non-NULL values in a column. A SELECT expression using COUNT(*) returns the number of rows that matched the query. Use COUNT(1) to get the same result.

How do I stop allow filtering in Cassandra?

((startdate,enddate,(id)) - keeping the start and end as partition and id as clustering - if your requirement is only the above query but again it will depend on how much data you will have in each range of dates - or else can you explain more about the requirement and nature of data ?

How do I add a secondary index in Cassandra?

Using CQL to create a secondary index on a column after defining a table. Using CQL, you can create an index on a column after defining a table. You can also index a collection column. Secondary indexes are used to query a table using a column that is not normally queryable.


1 Answers

This request also doesn't work:

That's because you are mis-understanding how sort order works in Cassandra. Instead of using a secondary index on firstname, create a table specifically for this query, like this:

CREATE TABLE usersByFirstName (
  id int,
  firstname text,
  lastname text,
  PRIMARY KEY (firstname,id));

This query should now work:

SELECT * FROM usersByFirstName WHERE firstname='francois'
ORDER BY id DESC LIMIT 5;

Note, that I have created a compound primary key on firstname and id. This will partition your data on firstname (allowing you to query by it), while also clustering your data by id. By default, your data will be clustered by id in ascending order. To alter this behavior, you can specify a CLUSTERING ORDER in your table creation statement:

WITH CLUSTERING ORDER BY (id DESC)

...and then you won't even need an ORDER BY clause.

I recently wrote an article on how clustering order works in Cassandra (We Shall Have Order). It explains this, and covers some ordering strategies as well.

like image 181
Aaron Avatar answered Sep 18 '22 01:09

Aaron