首页 > 解决方案 > 与 Cassandra 数据建模相关

问题描述

要求:表在 Cassandra 中。我们需要下表中从未成功完成的作业的部分编号:

CREATE TABLE SAMPLE.ALERTS (job_id UUID, section text, result text, PRIMARY KEY(job_id,section));

INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0100','successful');
INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0100','successful');

INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0200','successful');
INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0200','failed');

INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0300','failed');
INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0300','successful');

INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0400','failed');
INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0400','failed');

INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0700','failed');
INSERT INTO  SAMPLE.ALERTS (job_id, section, result) values (now(),'0700','failed');

所以在输出中我们想要节号 0400 和 0700。无论如何我们可以通过改变表的结构和通过查询来实现它吗?

标签: cassandranosqldata-modelingdatastax-enterprise

解决方案


通常,此类事情是通过添加物化视图或二级索引来完成的,但在您的情况下,这不是最佳选择,因为您没有很多不同的result值......

如果您使用 DSE,那么您可以使用 DSE 搜索来执行此类查询(但要考虑到此类查询的延迟将高于普通 Cassandra 查询)。

另一种可能性 - 只需添加第二个表,您将只写入失败的请求(作为原始数据的副本)。


推荐阅读