2

以下按预期工作。但是我应该执行谁来执行范围查询,例如“年龄 > 40 和年龄 < 50”

create keyspace Keyspace1;
use Keyspace1;
create column family Users with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type;

set Users[jsmith][first] = 'John';
set Users[jsmith][last] = 'Smith';
set Users[jsmith][age] = long(42);

get Users[jsmith];
=> (column=age, value=42, timestamp=1341375850335000)
=> (column=first, value=John, timestamp=1341375827657000)
=> (column=last, value=Smith, timestamp=1341375838375000)
4

2 回答 2

3

在 Cassandra 中执行此操作的最佳方法取决于您的要求,但支持这些类型的范围查询的方法非常相似。

Basically, you will take advantage of the fact that columns within a row are sorted by their names. So, if you use an age as the column name (or part of the column name), the row will be sorted by ages.

You will find a lot of similarities between this and storing time-series data. I suggest you take a look at Basic Time Series with Cassandra for the fundamentals, and the second half of an intro to the latest CQL features that gives an example of a somewhat more powerful approach.

The built-in secondary indexes are basically designed like a hash table, and don't work for range queries unless that range expression accompanies an equality expression on an indexed column. So, you could ask for select * from users where name = 'Joe' and age > 54, but not simply select * from users where age > 54, because that would require a full table scan. See Secondary Indexes doc for more details.

于 2012-07-05T04:36:02.753 回答
1

您必须在列年龄上创建二级索引:

update column family Users with column_metadata=[{column_name: age, validation_class: LongType, index_type: KEYS}];

然后使用:

get Users where age > 40 and age < 50 

注意:我认为:从 1.2 开始不支持独占运算符。

Datastax 有一个很好的文档:http ://www.datastax.com/dev/blog/whats-new-cassandra-07-secondary-indexes或者您可以创建和维护自己的二级索引。这是一个很好的链接: http ://www.anuff.com/2010/07/secondary-indexes-in-cassandra.html

于 2012-07-04T10:25:46.300 回答