Question

I am new to Cassandra and am wondering, how to solve this sensor data problem:

Every sensor has a name and a description, and multiple timestamps with a value each. In JSON it would look like this:

[{
  "sensor"      : "Sensor1",
  "description" : "first sensor",
  "timeseries"  : {
      1200001 : 2,
      1200002 : 5,
      1200006 : 1,
      1200009 : 3,
      1200011 : 7,
      1200012 : 9 }
}]

How can I save this with CQL? I tried something like this:

CREATE TABLE sensordata (
    sensor text,
    description text,   
    time int,           
    value int,          
    PRIMARY KEY (sensor, time)
);

But this would save a description for each timestamp. And if I add the description as Cluster Key, I always have to provide it while writing. My writing should look like this:

INSERT INTO sensordata (sensor, description) VALUES ('Sensor1', 'first sensor');
INSERT INTO sensordata (sensor, time, value) VALUES ('Sensor1', 1200001, 2);
INSERT INTO sensordata (sensor, time, value) VALUES ('Sensor1', 1200002, 5);
INSERT INTO sensordata (sensor, time, value) VALUES ('Sensor1', 1200006, 1);

Probably I should simply use two tables?

CREATE TABLE sensormetadata (
    sensor text,
    description text,
    PRIMARY KEY (sensor)
);
CREATE TABLE sensordata (
    sensor text,
    time int,           
    value int,          
    PRIMARY KEY (sensor, time)
);

In the end I want to query for one specific sensor (in a defined time range). The other query would be all sensor data from the last 120 seconds.

SELECT description, time, value from sensordata WHERE sensor='Sensor1';
SELECT description, time, value from sensordata WHERE sensor='Sensor1' AND time > 1200000 AND time < 1250000;
SELECT sensor, description, time, value from sensordata WHERE time > 1200000;

Thank you!

Best, Malte

Était-ce utile?

La solution

You can use your first schema, but make the description column static:

CREATE TABLE sensordata (
    sensor text,
    description text static,
    time int,           
    value int,          
    PRIMARY KEY (sensor, time)
);

This essentially means that the description will be stored once per partition (or once per sensor ID), but will appear in every row. Static columns were added in 2.0.6 and you can read more about them here: http://www.datastax.com/dev/blog/cql-in-2-0-6

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top