我在Cassandra有桌子:
CREATE TABLE test (
bucket int,
start timestamp,
end timestamp,
PRIMARY KEY((bucket),start, end)
);
我想得到一个类似的查询:
SELECT * FROM test where bucket = 1 and start <= current_time and end >= current_time
换句话说,我想找到包含给定时间戳的间隔。
我知道查询错了。我也尝试使用多列切片限制,但在这种情况下它也没用。有没有办法做到这一点?
答案 0 :(得分:2)
我过去也遇到过类似的问题。我这样做的方法是使用一列来跟踪开始/结束。然后有一行用于事件的开始,一行用于结束。让我们尝试创建一个表来存储&#34; Nerd Holidays,&#34;像这样:
CREATE TABLE nerd_holidays (
month_bucket int,
event_time timestamp,
beginend text,
name text,
PRIMARY KEY ((month_bucket), event_time, beginend)
) WITH CLUSTERING ORDER BY (event_time DESC, beginend ASC);
我会插入一些行:
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (3,'2018-03-14 00:00:00','begin','Pi Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (3,'2018-03-14 23:59:59','end','Pi Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (5,'2018-05-04 00:00:00','begin','Star Wars Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (5,'2018-05-04 23:59:59','end','Star Wars Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-19 00:00:00','begin','Talk Like a Pirate Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-19 23:59:59','end','Talk Like a Pirate Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-25 00:00:00','begin','Hobbit Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-25 23:59:59','end','Hobbit Day');
现在我可以查询9月份特定时间的数据,如下所示:
cassdba@cqlsh:stackoverflow> SELECT * FROM nerd_holidays
WHERE month_bucket=9
AND event_time >= '2018-09-18 00:00'
AND event_time <= '2018-09-19 08:33' ;
month_bucket | event_time | beginend | name
--------------+---------------------------------+----------+------------------------
9 | 2018-09-19 05:00:00.000000+0000 | begin | Talk Like a Pirate Day
(1 rows)
正如你所看到的,&#34;像海盗日那样谈话&#34;在请求的日期范围内开始。