I have designed following table to store server alarms:
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)
);
Let's enter some data:
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');
The query my application will be running are:
//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';
I don't know if I should prepare more table example : host_alerts_by_hostname or host_alerts_by_cleartime and so on or simply add clustering index. As unique id is the only unique column but I need to retrive data from other column
Not cleared alarms: '1970-01-01 00:00:00+0530' cleared event has some date value.
host_id is server name
occur_time is when event has occured.
last_occur is time when event again reoccured.
alarm_name is what has happend with system.
How can i model my table so that I can perform these query and update based on unique_id? With what I have tried select is not possible and during upsert new row is created for same unique_key.