cassandra get all records in time range

后端 未结 3 604
梦毁少年i
梦毁少年i 2020-11-29 04:22

I have to work with a column family that has (user_id, timestamp) as key. In my query I would like to fetch all records in a given time range independent of the user_id. Thi

3条回答
  •  自闭症患者
    2020-11-29 05:02

    It appears the hotness for being able to query by time (or any range) is to specify some "other column" as your Partition key, and then specify timestamp as a "clustering column"

    CREATE TABLE postsbyuser (
         userid bigint,
         posttime timestamp,
         postid uuid,
         postcontent text,
         PRIMARY KEY ((userid), posttime)
       ) WITH CLUSTERING ORDER BY (posttime DESC);
    

    insert fake data

      insert into postsbyuser (userid, posttime) values (77, '2013-04-03 07:04:00');
    

    and query (the important part being that it is a "fast" query and ALLOW FILTERING is not required, which is how it should be):

      SELECT * FROM postsbyuser where userid=77 and posttime > '2013-04-03 07:03:00' and posttime < '2013-04-03 08:04:00';
    

    You can also use tricks to group by day (and thus be able to query by day) or what not.

    If you use the "group by day" style trick then a secondary index would also be an option (though secondary indexes seem to only work with "EQ" = operator?).

提交回复
热议问题