我正在与 cassandra 和 CQL 作斗争。我正在定义这个表架构,
CREATE TABLE useradvisits (
userid text,
adid int,
type int,
timestamp timestamp,
PRIMARY KEY (userid, adid, type)
);
我想这样查询,
SELECT * from useradvisits where userid = 'user-1' and adId in (100, 1001);
但它说, 错误请求:PRIMARY KEY 部分 adid 不能受 IN 关系限制
我正在使用最新的 datstax enterprise 版本 3.1。
cqlsh 控制台显示,
[cqlsh 3.1.2 | Cassandra 1.2.6.1 | CQL spec 3.0.0 | Thrift protocol 19.36.0]
因为我需要 (adId + type) 是唯一的,即 (100 : 1), (100:2) 两者都对用户存在。谁能帮我解决方法吗?
最佳答案
您根据 adid 和 type 创建了复合列键。您可以将其中之一添加到行键来创建组合。只需使用 PRIMARY KEY ((userid, adid), type)
创建一个模型,您的查询就会起作用。这种方法的缺点是:您始终必须指定 adid。
您也可以执行相反的操作,并将类型添加到行键。如果您提前知道“类型”的范围,效果会更好。
关于cassandra - CQL SELECT 无法处理复合列的 "in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18248804/