gpt4 book ai didi

Cassandra:无法使用聚簇键限制 2 列(版本 2.1.9)

转载 作者:行者123 更新时间:2023-12-02 01:33:32 28 4
gpt4 key购买 nike

我有一个与此非常相似的架构:-

create table x(id int, start_date timestamp, end_date timestamp, 
primary key((id), start_date, end_date))
with clustering order by (start_date desc, end_date desc);

现在我遇到了一个问题,我必须在开始日期和结束日期之间进行查询。像这样:-

select count(*) from x where id=2 and start_date > 'date' and end_date < 'date' ;

但它给了我一个类似于以下的错误:-

InvalidRequest: code=2200 [Invalid query] message="PRIMARY KEY column "end_date" 
cannot be restricted (preceding column "start_date" is restricted
by a non-EQ relation)"

我是 cassandra 的新手,欢迎任何和所有建议,即使它需要我们进行架构更改。 :)

最佳答案

你不说你运行的是哪个版本的Cassandra,但是在2.2及以后你可以对聚类列做多列切片限制。这可以接近你想要的。 CQL 中的语法有点难看,但基本上您必须指定起始范围以及指定的所有聚簇列,就像复合键一样。请务必考虑首先按第一列排序的行,然后再按第二列排序的行。

假设我们有这些数据:

SELECT * from x;

id | start_date | end_date
----+--------------------------+--------------------------
2 | 2015-09-01 09:16:47+0000 | 2015-11-01 09:16:47+0000
2 | 2015-08-01 09:16:47+0000 | 2015-10-01 09:16:47+0000
2 | 2015-07-01 09:16:47+0000 | 2015-09-01 09:16:47+0000
2 | 2015-06-01 09:16:47+0000 | 2015-10-01 09:16:47+0000

现在让我们根据两个日期来选择:

SELECT * from x where id=2 
and (start_date,end_date) >= ('2015-07-01 09:16:47+0000','2015-07-01 09:16:47+0000')
and (start_date,end_date) <= ('2015-09-01 09:16:47+0000','2015-09-01 09:16:47+0000');

id | start_date | end_date
----+--------------------------+--------------------------
2 | 2015-08-01 09:16:47+0000 | 2015-10-01 09:16:47+0000
2 | 2015-07-01 09:16:47+0000 | 2015-09-01 09:16:47+0000

现在您会注意到其中一个结束日期似乎晚于我们的限制,但事实并非如此。由于事物首先按 start_date 排序,您将获得具有匹配 start_date 的所有结束日期,因为它们在复合范围的范围内。要删除这样的行,您可能需要在客户端进行一些过滤。

查看更多信息here ,在“多列切片限制”下。

关于Cassandra:无法使用聚簇键限制 2 列(版本 2.1.9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com