开发者

cassandra secondary index return results in lexical rowkey order, even with RandomPartitioner?

As far as I understand, a Cassandra secondary index is stored as an internal CF, where the rowkeys are the values within the index, and the columns are rowkeys back to the original CF being indexed.

Is it possible to have the columns of the index store the original CF rowkey values? Then, since columns within the index row are sorted, a query for a particular value in the index theoretically could return rowkeys in sorted value order.

This is how I would do it if I was to manually maintain my own index CF (I'd have my manual index CF sort its columns as strings), I'm curious if the same can be done with built-in secondary indexes.


A hopefully clarifying example... I have 5 rows with 2 columns each (identifier is to easily distinguish the rows, birth_date is being indexed), each row with a UTF8 key (in this case a single char string):

[default@demo] create column family users with comparator=UTF8Type
...     and column_metadata=
...     [{column_name: identifier, validation_class: LongType}
...     ,{column_name: birth_date, validation_class: LongType, inde开发者_JAVA百科x_type: KEYS}];
86518c00-e9f7-11e0-0000-242d50cf1fde
Waiting for schema agreement...
... schemas agree across the cluster
[default@demo] set users['a']['identifier'] = 1;
Value inserted.
[default@demo] set users['a']['birth_date'] = 1975;
Value inserted.
[default@demo] set users['c']['identifier'] = 3;
Value inserted.
[default@demo] set users['c']['birth_date'] = 1975;
Value inserted.
[default@demo] set users['b']['identifier'] = 2;
Value inserted.
[default@demo] set users['b']['birth_date'] = 1975;
Value inserted.
[default@demo] set users['x']['identifier'] = 5;
Value inserted.
[default@demo] set users['x']['birth_date'] = 1975;
Value inserted.
[default@demo] set users['f']['identifier'] = 4;
Value inserted.
[default@demo] set users['f']['birth_date'] = 1975;
Value inserted.

Now when I make an index query, I get the users rows back in what appears to be reverse order of their rowkeys' md5 hashes (looking at the identifier, the result order is x,b,f,c,a):

[default@demo] get users where birth_date = 1975;
-------------------
RowKey: ff
=> (column=birth_date, value=1975, timestamp=1317231030507000)
=> (column=identifier, value=5, timestamp=1317231030504000)
-------------------
RowKey: 0b
=> (column=birth_date, value=1975, timestamp=1317231030502000)
=> (column=identifier, value=2, timestamp=1317231030500000)
-------------------
RowKey: 0f
=> (column=birth_date, value=1975, timestamp=1317231031992000)
=> (column=identifier, value=4, timestamp=1317231030509000)
-------------------
RowKey: 0c
=> (column=birth_date, value=1975, timestamp=1317231030498000)
=> (column=identifier, value=3, timestamp=1317231030494000)
-------------------
RowKey: 0a
=> (column=birth_date, value=1975, timestamp=1317231030491000)
=> (column=identifier, value=1, timestamp=1317231030476000)

5 Rows Returned.

My question is, is there a way to have the internal index CF use 'a', 'b', 'c', 'f', 'x' as its column names, so that when I make an index query, I get back the users rows in lexical rowkey order.


The reason you can't do this is, the index ordering has to match the partitioner ordering, or you couldn't "page" through resultsets across multiple nodes (without having to do scatter/gather for each query, anyway).

We do have https://issues.apache.org/jira/browse/CASSANDRA-1599 open to allow custom ordering, so you should watch that issue for updates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜