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

Is it possible to find entries in Kassandra whose primary key corresponds to an arbitrary subset of all fields of the primary key?

Example:

Using the table described below, you can find records in which the primary key has a special type and name without specifying id or size ?

 CREATE TABLE playlists ( id uuid, type text, name text, size int, artist text, PRIMARY KEY (id, type, name, size) ); 

Thanks!

+4
source share
1 answer

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

If you try to do this:

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

You will get 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:

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

In your example, Cassandra will allow you to execute 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 this helps

+15
source

Source: https://habr.com/ru/post/1481045/


All Articles