Chris Dutrow
Chris Dutrow

Reputation: 50362

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!

Upvotes: 5

Views: 8051

Answers (1)

Sergio Ayestarán
Sergio Ayestarán

Reputation: 5890

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

Upvotes: 17

Related Questions