DataStax Academy FAQ

DataStax Academy migrated to a new learning management system (LMS) in July 2020. We are also moving to a new Cassandra Certification process so there are changes to exam bookings, voucher system and issuing of certificates.

Check out the Academy FAQ pages for answers to your questions:


question

qiuzeliang_194033 avatar image
qiuzeliang_194033 asked ·

cassandra 表中存在map字段时,不能使用in来查询吗?

cassandra 表中存在map字段时,不能使用in来查询吗? 有其他好的方式可以解决吗?

data modeling
11111.png (66.2 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

bonian.hu_177317 avatar image
bonian.hu_177317 answered ·

在4.0 版本之前的Cassandra , 这样的查询是不支持的。 这里是Jira:

https://issues.apache.org/jira/browse/CASSANDRA-12654


目前的解决方案有两个:

1) 在查询的时候注明列表名, 如案例中的方法:

SELECT convid, convmsgid FROM message_map WHERE id = '100' AND convmsgid in (1,4);

2) 给map属性的数据加上frozen type,之后使用 * 查询。 如:

CREATE TABLE if NOT EXISTS message_map(
    convid text,    
    convmsgid bigint,    
    attr frozen<map<text, text>>,    
    primary key (convid, convmsgid))     
WITH CLUSTERING ORDER BY (convmsgid DESC );

https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/collection_type_r.html

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered ·

我无法复制您报告的问题。 这是我的schema:

CREATE TABLE community.message_map (
    convid text,
    convmsgid bigint,
    attr map<text, text>,
    PRIMARY KEY (convid, convmsgid)
) WITH CLUSTERING ORDER BY (convmsgid DESC)

我插入了您提供的相同数据:

INSERT INTO message_map (convid, convmsgid, attr) VALUES ('100', 1, {'key':'value'});
INSERT INTO message_map (convid, convmsgid, attr) VALUES ('100', 2, {'key':'value'});
INSERT INTO message_map (convid, convmsgid, attr) VALUES ('100', 3, {'key':'value'});
INSERT INTO message_map (convid, convmsgid, attr) VALUES ('100', 4, {'key':'value'});
INSERT INTO message_map (convid, convmsgid, attr) VALUES ('100', 5, {'key':'value'});

我可以在cqlsh中检索数据:

cqlsh:community> SELECT * FROM message_map WHERE convid='100' AND convmsgid IN (1, 4);

 convid | convmsgid | attr
--------+-----------+------------------
    100 |         4 | {'key': 'value'}
    100 |         1 | {'key': 'value'}

我也可以检索notebooks中的数据:

[UPDATE] 抱歉。 Bonian是正确的。

我正在Cassandra 4.0上运行测试。


c7220-studio.png (58.2 KiB)
Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.