Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra - search by primary key using an arbitrary subset of the primary key columns

Is it possible to find records in Cassandra who's primary key matches an arbitrary subset of all of the primary key fields?

Example:

Using the table described below, it is possible to find the records whos's primary key has a particular type and name without specifying an id or size?

CREATE TABLE playlists (
  id      uuid,
  type    text,
  name    text,
  size    int,

  artist text,

  PRIMARY KEY (id, type, name, size)
);

Thanks!

like image 668
Chris Dutrow Avatar asked Dec 05 '22 11:12

Chris Dutrow


1 Answers

At least in Cassandra 1.2 it is possible but it is disabled by default,

If you try to make this:

SELECT * from playlist where type = 'sometype' and name = 'somename';

You will receive this error:

Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING

Then you can enable it running this:

SELECT * from playlist where type = 'sometype' and name = 'somename' ALLOW FILTERING;

In your example Cassandra will allow you to do queries using a complete subset of the primary key from left to right, for example:

SELECT * from playlist where id = 'someid'; (ALLOWED)
SELECT * from playlist where id = 'someid' and type = 'sometype'; (ALLOWED)
SELECT * from playlist where id = 'someid' and type = 'sometype' and name = 'somename'; (ALLOWED)
SELECT * from playlist where id = 'someid' and type = 'sometype' and name = 'somename' and size=somesize; (ALLOWED)

Hope it helps

like image 90
Sergio Ayestarán Avatar answered Dec 11 '22 12:12

Sergio Ayestarán