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

别等时光非礼了梦想. 提交于 2019-12-06 04:13:27

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/16575572/cassandra-search-by-primary-key-using-an-arbitrary-subset-of-the-primary-key-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!