本文介绍了在Cassandra中使用除了Row Key之外的复合键进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CQL3中除了Row Key之外的组合键来查询数据过滤。
这些是我的查询:

I want to query data filtering by composite keys other than Row Key in CQL3. These are my queries:

CREATE TABLE grades (id int,
  date timestamp,
  subject text,
  status text,
  PRIMARY KEY (id, subject, status, date)
);

当我尝试访问数据时,

SELECT * FROM grades where id = 1098; //works fine
SELECT * FROM grades where subject = 'English' ALLOW FILTERING; //works fine
SELECT * FROM grades where status = 'Active' ALLOW FILTERING; //gives an error



只是为了实验,我改变了键,保持'id'作为我的主行键始终。我总是只能够使用主行键或第二个键查询,考虑上面的例子,如果我在主键列表中交换主题和状态,然后我可以查询状态,但我得到类似的错误,如果我尝试做主题或时间。

Just to experiment, I shuffled the keys around keeping 'id' as my Primary Row Key always. I am always ONLY able to query using either the Primary Row key or the second key, considering above example, if I swap subjects and status in Primary Key list, I can then query with status but I get similar error if I try to do by subject or by time.

我做错了什么?我可以不使用CQL3中的任何其他复合键来查询数据?
我使用Cassandra 1.2.6和CQL3。

Am I doing something wrong? Can I not query data using any other composite key in CQL3? I'm using Cassandra 1.2.6 and CQL3.

推荐答案

这看起来一切正常的行为,根据Cassandra复合键模型( http://www.datastax.com/docs/1.2/cql_cli/cql/SELECT)。 Cassandra数据模型旨在(并且这是一种通用的NoSQL思维方式)在授予查询是高性能的,这是对存储和索引您的数据的方式的限制的费用,然后如何查询, 总是需要限制主题的前面部分。

That looks all normal behavior according to Cassandra Composite Key model (http://www.datastax.com/docs/1.2/cql_cli/cql/SELECT). Cassandra data model aims (and this is a general NoSQL way of thinking) at granting that queries are performant, that comes to the expense of "restrictions" on the way you store and index your data, and then how you query it, namely you "always need to restrict the preceding part of subject" on the primary key.

您不能在查询的主键列表上交换元素(这更多是一种SQL思维方式)。如果要使用复合键的多个元素,则总是需要约束/限制主键的上一个元素。这意味着,如果你有复合key =(id,主题,状态,日期)和想要查询状态,你将需要限制id和/或主题 允许过滤,即,你可以只限制主题,不需要限制id)。所以,如果你想查询状态,你将能够以两种不同的方式查询:

You cannot swap elements on the primary key list on the queries (that is more a SQL way of thinking). You always need to "Constraint"/"Restrict" the previous element of the primary key if you are to use multiple elements of the composite key. This means that if you have composite key = (id, subject, status, date) and want to query "status", you will need to restrict "id" and/or "subject" ("or" is possible in case you use "allow filtering", i.e., you can restrict only "subject" and do not need to restrict "id"). So, if you want to query on "status" you will b able to query in two different ways:

第一个是针对特定的学生,第二个针对状态=活动的主题上的所有学生。

The first is for a specific "student", the second for all the "students" on the subject in status = "Active".

这篇关于在Cassandra中使用除了Row Key之外的复合键进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:19