I have the following table to store time-series data:
CREATE TABLE alerts_by_year_day (
day_of_year int,
year int,
alert_timestamp timestamp,
serial_number text,
alert_id uuid,
alert_type text,
....
....
PRIMARY KEY((year, day_of_year), alert_timestamp, serial_number, alert_id)
) WITH CLUSTERING ORDER BY (alert_timestamp DESC, serial_number DESC);
- For the UI report I want to retrieve all the alerts for a given period of time. I have this query:
select * from alerts_by_year_day where year = 2015 and day_of_year in (241, 240);
But this query is returning me results that are in the ASC order of the year and then ASC order of day. So results like this
2015 | 240 |.....
2015 | 241 |.....
But I want to display the latest results first or in the descending order. Adding 'order by alert_timestamp;' gives an error So how can I display results that are in the descending order ?
- Then for a given period of time I want to retrieve only certain types of alert based on alert_type.
So I created a mat view like this:
CREATE MATERIALIZED VIEW alerts_by_type_and_timestamp AS
SELECT *
FROM alerts_by_year_day
WHERE alert_timestamp IS NOT NULL AND
alert_type IS NOT NULL AND
day_of_year IS NOT NULL AND
year IS NOT NULL AND serial_number IS NOT NULL AND
alert_id IS NOT NULL
PRIMARY KEY ((year, day_of_year, alert_type), alert_timestamp, serial_number, alert_id)
WITH CLUSTERING ORDER BY (alert_timestamp DESC, serial_number DESC, alert_id DESC);
But of course it returns results by type first and in that by timestamp. What I am looking for is only a subset of types and in the desc order they were generated in. Is that possible in Cassandra ?
Thanks