简体   繁体   中英

query to get records by multiple ranges in cassandra

I have following model:

CREATE TABLE IF NOT EXISTS log (
  date varchar,
  timeid timeuuid,
  message varchar,
  ip varchar,
  time timestamp,
  user bigint,
  file varchar,
  line int,
  func varchar,
  level int,
  PRIMARY KEY (date, timeid, time)
) WITH CLUSTERING order by (timeid DESC);

How I can get all logs of some day, where record is between given timeid and time values?

Currently problem is because if I use timeid>something, I cannot use time>something, because cassandra doesn't allow multiple filtering (non-EQ) on clustering keys.

I tried something like

SELECT * FROM log 
    WHERE date='2014-09-14' 
    AND (timeid, time) <= (1245a230-3baa-11e4-8ca7-4bdg1fe06d46, '2014-09-14 03:57:16+0200') 
    AND (timeid, time) >= (cb66eef0-3ba9-11e4-8ca7-4bd6sfe06d46, '2015-09-14 02:57:14+0200');

but in this case cassandra is not restricting time, so still I will get values even before 2015-09-14 02:57:14+0200, so it is just looking for timeid value.

Does anyone know how to solve this?

TimeUUID's have the timestamp encoded in them, so unless the timestamp you are using in your timeuuid is different than the time value, you should be ok just filtering based on timeid.

Not only that, but cql has some builtin functions to make this easier for example

SELECT * FROM myTable
   WHERE t > maxTimeuuid('2013-01-01 00:05+0000')
   AND t < minTimeuuid('2013-02-02 10:00+0000')

Reference Datastax Documentation on TimeUUID functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM