Cassandra time based query

若如初见. 提交于 2019-12-23 11:56:25

问题


I have the following Cassandra table which records the user access to a web page.

create table user_access (
    id timeuuid primary key,
    user text,
    access_time timestamp
);

and would like to do a query like this:
get the list of users who access the page for more than 10 times in the last hour.

Is it possible to do it in Cassandra? (I'm kind of stuck with the limited CQL query functionalities)
If not, how do I remodel the table to do this?


回答1:


Can you do it? yes. Can you do it efficiently? I'm not convinced.

It's not clear what the timeuuid you are using represents.

You could reorganize this to

CREATE TABLE user_access (
  user text,
  access_time timestamp,
  PRIMARY KEY (user_id, access_time)
);

SELECT COUNT(*)
FROM user_access
WHERE user_id = '101'
  AND access_time > 'current unix timestamp - 3600'
  AND access_time < 'current unix timestamp';

Then filter the results on your own in your language of choice. I wouldn't hold your breathe waiting for sub query support.

That's going to be horribly inefficient if you have lots of users though.

There may be a better solution using cql's counter columns and binning accesses to the start of the hour. That could get you per hour accesses, but that's not the same as within the last hour.



来源:https://stackoverflow.com/questions/19103004/cassandra-time-based-query

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