cassandra - 建模cassandra表以进行upsert和select查询

标签 cassandra cassandra-2.0

我设计了下表来存储服务器警报:

create table IF NOT EXISTS host_alerts(
    unique_key text,
    host_id text,
    occur_time timestamp,
    clear_time timestamp,
    last_occur timestamp,
    alarm_name text,
    primary key (unique_key,host_id,clear_time)
);

让我们输入一些数据:
truncate host_alerts;

insert into host_alerts(unique_key,host_id,alarm_name,
    clear_time,occur_time,last_occur
) 
values('1','server-1','disk failure',
'1970-01-01 00:00:00+0530','2015-07-01 00:00:00+0530','2015-07-01 00:01:00+0530');

insert into host_alerts(unique_key,host_id,alarm_name,
    clear_time,occur_time,last_occur
) 
values('1','server-1','disk failure',
'1970-01-01 00:00:00+0530','2015-07-01 00:00:00+0530','2015-07-01 00:02:00+0530');

insert into host_alerts(unique_key,host_id,alarm_name,
    clear_time,occur_time,last_occur
) 
values('1','server-1','disk failure',
'2015-07-01 00:02:00+0530','2015-07-01 00:00:00+0530','2015-07-01 00:02:00+0530');

我的应用程序将要运行的查询是:
//All alarms which are **not cleared** for host_id
select * from host_alerts where  host_id = 'server-1' and clear_time = '1970-01-01 00:00:00+0530';

//All alarms which are  cleared for host_id
select * from host_alerts where  host_id = 'server-1' and clear_time > '2015-07-01 00:00:00+0530';

//All alarms between first occurrence
select * from host_alerts where  host_id = 'server-1' 
and occur_time > '2015-07-01 00:02:00+0530'and occur_time < '2015-07-01 00:05:00+0530';

我不知道是否应该准备更多表示例:host_alerts_by_hostname
或host_alerts_by_cleartime等,或仅添加聚簇索引。 因为唯一ID是唯一的唯一列,但我需要从其他列中检索数据

未清除警报:'1970-01-01 00:00:00 + 0530'已清除事件具有某些日期值。

host_id 是服务器名称

appear_time 是事件发生的时间。

last_occur 是事件再次发生的时间。

alarm_name 是系统发生的情况。

如何为我的表建模,以便我可以基于unique_id执行这些查询和更新?用我尝试过的方法,选择是不可能的,并且在向上插入期间,为相同的unique_key创建了新行。

最佳答案

我认为您可能需要三个表来支持三种查询类型。

第一个表将支持有关每个主机何时发生警报的历史记录的时间范围查询:

CREATE TABLE IF NOT EXISTS host_alerts_history (
    host_id text,
    occur_time timestamp,
    alarm_name text,
    PRIMARY KEY (host_id, occur_time)
);

SELECT * FROM host_alerts_history WHERE host_id = 'server-1' AND occur_time > '2015-08-16 10:05:37-0400';

第二个表将跟踪每个主机的未清除警报:
CREATE TABLE IF NOT EXISTS host_uncleared_alarms (
    host_id text,
    occur_time timestamp,
    alarm_name text,
    PRIMARY KEY (host_id, alarm_name)
);

SELECT * FROM host_uncleared_alarms WHERE host_id = 'server-1';

最后一个表将跟踪何时清除每个主机的警报:
CREATE TABLE IF NOT EXISTS host_alerts_by_cleartime (
    host_id text,
    clear_time timestamp,
    alarm_name text,
    PRIMARY KEY (host_id, clear_time)
);

SELECT * FROM host_alerts_by_cleartime WHERE host_id = 'server-1' AND clear_time > '2015-08-16 10:05:37-0400';

当新的警报事件到达时,您将执行以下批处理:
BEGIN BATCH
INSERT INTO host_alerts_history (host_id, occur_time, alarm_name) VALUES ('server-1', dateof(now()), 'disk full');
INSERT INTO host_uncleared_alarms (host_id, occur_time, alarm_name) VALUES ('server-1', dateof(now()), 'disk full');
APPLY BATCH;

请注意,由于时间戳记不是键的一部分,因此插入到未清除的表中是一个upsert。因此,该表对于每个警报名称仅具有一个条目,并带有最近一次发生的时间戳。

当警报清除事件到达时,您将执行以下批处理:
BEGIN BATCH
DELETE FROM host_uncleared_alarms WHERE host_id = 'server-1' AND alarm_name = 'disk full';
INSERT INTO host_alerts_by_cleartime (host_id, clear_time, alarm_name) VALUES ('server-1', dateof(now()), 'disk full');
APPLY BATCH;

我不太了解您的“unique_key”是什么或它来自何处。我不确定是否需要它,因为host_id和alarm_name的组合应该是您要使用的粒度级别。在组合中添加另一个唯一键可能会引起许多无与伦比的警报/清除事件。如果unique_key是警报ID,则在我的示例中使用该ID代替alarm_name,并将alarm_name作为数据列。

为了防止表随着时间的推移用旧数据填满,可以使用TTL功能在几天后自动删除行。

关于cassandra - 建模cassandra表以进行upsert和select查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892310/

相关文章:

node.js - cassandra Node js 驱动程序的executeAsync 方法

java - Cassandra Java 驱动程序 : how many contact points is reasonable?

database-design - 在 Cassandra 中拥有多个键空间是一种好习惯吗?

cassandra - 如何在 cassandra 3.11.9+dynamic_snitch_badness_threshold cassandra.yaml 参数上禁用dynamic_snitch

python - 在 Windows 10 上安装 cassandra 3.10

python - Python多线程程序中如何解析 "cassandra.cluster.NoHostAvailable"

apache-spark - 使用 datastax 连接器连接到 cassandra

cassandra - cassandra 中的节点是什么意思?

cassandra - 为什么 Cassandra 不加载 CSV 文件中的所有值?

Cassandra Cql 范围选择