thelastchief
thelastchief

Reputation: 211

Cassandra: Set collection boolean, how to?

I am making a columnfamily that will save values of different sensors.

Sensor_04 will have boolean values of 4 different doors. door1, door2, door3,door4.

The goal is to be able to query and ask for is door1,2,3 or 4 true or false?

How is the syntax done for this? Cause i know my example is false:

CREATE COLUMNFAMILY lockSystem (sID int, sNamn text, doors set<boolean>, PRIMARY KEY(sID));

Wrong --->

 INSERT INTO lockSystem (sID, sNamn, doors) VALUES (4,'Sensor_04' {'door1:True','door2:False','door3:False','door4:false'});

I hope my question makes sense, my goal is something like this:

Sensor_4: sID int, sName text, set: door1 bool, door2 bool, door3 bool, door4 bool

Upvotes: 1

Views: 209

Answers (1)

xmas79
xmas79

Reputation: 5180

You could assume that if a door is present in your set then its value is true, and then yuo can use the filtering capabilities of C* to query your data.

So I'd change the model to something like:

CREATE TABLE lockSystem (
    sID int, 
    sNamn text, 
    doors set<text>, 
    PRIMARY KEY(sID)
);

where the doors set has been changed to a set of text. You then add data into your set when one door alarm gets "high" with:

UPDATE lockSystem SET doors = doors + { '1' } WHERE sID = ?;

To filter you data you can then use:

SELECT * FROM lockSystem WHERE doors CONTAINS '1';

Have a look at the using the set type documentation, and on how to filter data in a collection.

Upvotes: 2

Related Questions