这是我用于聊天类应用程序的 cassandra 表:
CREATE TABLE tax_keyspace_dev.chat_messages (
message text,
when timestamp,
from_id text,
to_id text,
read boolean,
participants text,
PRIMARY KEY(participants, when, to_id)
);
此查询工作:
select * from tax_keyspace_dev.chat_messages where participants='caone@one.com_shashank_shrivastava@acme.com' order by when;
但以下查询不起作用:
select * from tax_keyspace_dev.chat_messages where to_id='caone@one.com' order by when;
错误是“错误的请求:无法限制主键部分 to_id(前面的部分不受限制或非 EQ 关系) ”
update tax_keyspace_dev.chat_messages set read=true where participants = 'caone@one.com_shashank_shrivastava@acme.com' and when = '2014-04-10 17:44:22+0530';
错误是“错误请求:缺少必需的 PRIMARY KEY 部分 to_id ”
如果我从复合键中删除“to_id”并创建单独的索引,如下所示:
CREATE TABLE tax_keyspace_dev.chat_messages (
message text,
when timestamp,
from_id text,
to_id text,
read boolean,
participants text,
PRIMARY KEY(participants, when)
);
CREATE INDEX idx_chat_messages_to ON tax_keyspace_dev.chat_messages (to_id);
然后其他查询工作,但这个失败:
select * from tax_keyspace_dev.chat_messages where to_id='caone@one.com' order by when;
出现错误“错误请求:不支持带有 2ndary 索引的 ORDER BY。 ”
我如何设计我的桌子,以便所有这些用例都可以工作?
select * from tax_keyspace_dev.chat_messages where participants='caone@one.com_shashank_shrivastava@acme.com' order by when;
update tax_keyspace_dev.chat_messages set read=true where participants = 'caone@one.com_shashank_shrivastava@acme.com' and when = '2014-04-10 17:44:22+0530';
select * from tax_keyspace_dev.chat_messages where to_id='caone@one.com' order by when;