3

这是我用于聊天类应用程序的 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;
4

1 回答 1

5

使用 cassandra 时,主键的第一部分成为分区键。因此,要转到特定分区以检索行,您需要始终指定具有等于约束的主键。

select * from tax_keyspace_dev.chat_messages where participants='caone@one.com_shashank_shrivastava@acme.com' order by when;

以下查询建议您到达名为“participants”的行分区,然后在使用 ASC 的默认排序时按顺序排序。此排序可能也不需要,因为您的列默认按升序排序。

select * from tax_keyspace_dev.chat_messages where to_id='caone@one.com' order by when; 

select * from tax_keyspace_dev.chat_messages where to_id='caone@one.com' order by when;

以下查询不起作用,因为您没有提供行分区来定位值。默认情况下,行分区键用于标识包含数据的 SSTable。因此,默认情况下,cassandra 不支持这种昂贵的操作。

发生的事情很简单。如果你错过了这个行分区键,cassandra 必须扫描所有的 SSTable 并从中取出数据。这可以通过使用ALLOW FILTERING来完成,但您的查询变得昂贵,因为它不会使用布隆过滤器。

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'; 

如果在 cassandra 上进行更新,它与插入没有什么不同。只需考虑使用地图操作的情况。您正在尝试修改一个值,但您没有地图的完整键。在内部,cassandra 将值存储为“participants_when_to_id”:值。

于 2014-04-11T12:39:21.900 回答