I am trying to display the latest values from a list of sensors. The list should also be sorted by timestamp.
I tried two different approaches. I turned on the sensor update time in the primary key:
CREATE TABLE sensors ( customerid int, sensorid int, changedate timestamp, value text, PRIMARY KEY (customerid, changedate) ) WITH CLUSTERING ORDER BY (changedate DESC);
Then I can select the list as follows:
select * from sensors where customerid=0 order by changedate desc;
which leads to the following:
customerid | changedate | sensorid | value ------------+--------------------------+----------+------- 0 | 2015-07-10 12:46:53+0000 | 1 | 2 0 | 2015-07-10 12:46:52+0000 | 1 | 1 0 | 2015-07-10 12:46:52+0000 | 0 | 2 0 | 2015-07-10 12:46:26+0000 | 0 | 1
The problem is that I am not getting only the latest results, but all the old values too.
If I remove the changes from the primary key, the selection will fail.
InvalidRequest: code=2200 [Invalid query] message="Order by is currently only supported on the clustered columns of the PRIMARY KEY, got changedate"
Sensor values are also not updated:
update overview set changedate=unixTimestampOf(now()), value = '5' where customerid=0 and sensorid=0; InvalidRequest: code=2200 [Invalid query] message="PRIMARY KEY part changedate found in SET part"
This fails because the change is part of the primary key.
Is there a way to save only the most recent values from each sensor, and also keep a table ordered by timestamp?
Edit: In the meantime, I tried a different approach to keep only the most recent value.
I used this circuit:
CREATE TABLE sensors ( customerid int, sensorid int, changedate timestamp, value text, PRIMARY KEY (customerid, sensorid, changedate) ) WITH CLUSTERING ORDER BY (changedate DESC);
Before pasting the last value, I will delete all the old values
DELETE FROM sensors WHERE customerid=? and sensorid=?;
But this fails because changedate NOT part of the WHERE clause.