Cassandra cql: how to select the LAST n rows from a table

纵然是瞬间 提交于 2019-11-29 09:33:00

You didn't specify last n "by what".

To get the last N per id:

SELECT * FROM option_data WHERE ts=1 ORDER BY id DESC LIMIT N;

ORDER BY clause can only be applied to the second column in a compound primary key. If you need to query by time you will need to think about your data model a little more.

If your queries are most often "last N", you might consider writing something like this:

CREATE TABLE time_series (
    id text,
    t timeuuid,
    data text,
    PRIMARY KEY (id, t)
) WITH CLUSTERING ORDER BY (t DESC)

... where 'id' is your time series id. The CLUSTERING ORDER reverses the order of timeuuid 't', causing the cells to be stored in a natural order for your query.

With this, you would get the last five events as follows:

SELECT * FROM time_series WHERE id='stream id' LIMIT 5;

There is a lot of information out there for time series in Cassandra. I suggest reading some of the more recent articles on the matter. This is concise and relatively recent: http://www.datastax.com/documentation/tutorials/Time_Series.pdf

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