1

I have defined table structure as defined below,

 CREATE TABLE sensor_data (
                asset_id text, 
                event_time timestamp,
                sensor_type int, 
                temperature int, 
                humidity int,
                voltage int,
                co2_percent int
                PRIMARY KEY(asset_id ,event_time)
            ) WITH CLUSTERING ORDER BY (event_time ASC)

this table captures data coming from a sensor and depending on type of sensor -- column sensor_type, some columns will have a value some others will not. Example temperature only applies to temperature sensor, humidity sensor applies to humidity sensor etc.

Now as I work with more and more sensor my intention is I will simply add additional columns using alter table command. Is this a correct strategy to follow or are there better ways to design this table for future use?

Subodh Nijsure
  • 3,305
  • 5
  • 26
  • 45

1 Answers1

2

I've answered to a similar question few hours ago: here

Assuming you're Cassandra 2.X ready your situation is easier to handle, to perform what you need I'd use a Map

CREATE TABLE sensor_data (
  asset_id text, 
  event_time timestamp,
  sensor_type int, 
  sensor_info map<text, int>,
  PRIMARY KEY(asset_id ,event_time)
) WITH CLUSTERING ORDER BY (event_time ASC)

Advantages is that your schema will remain the same even if new sensors come into your world. Disadvantage is that you won't be able to retrieve a specific data from your collection, you will always retrieve the collection in its entirely. If you're in Cassandra 2.1 secondary indexes on collections might help.

HTH, Carlo

Community
  • 1
  • 1
Carlo Bertuccini
  • 19,615
  • 3
  • 28
  • 39