Advertisement

Latest Post

Tuesday, 20 December 2016

How to Use Sets Collections Data Type in Cassandra Database

Sets Collections Data Type
A set is collection of unique values. You can define and insert a map

Syntax: - SET '<' cql_type '>'

Example:- 
cqlsh:payroll> CREATE TABLE images (
           ...     id int PRIMARY KEY,
           ...     name text,
           ...     owner text,
           ...     tags set<text> // A set of text values
           ... );
cqlsh:payroll> Select * from images;

 id | name | owner | tags
----+------+-------+------

(0 rows)
cqlsh:payroll> INSERT INTO images (id,name, owner, tags)
           ...  VALUES (785,'cat.jpg', 'alok', { 'pet', 'cute' });
cqlsh:payroll> select * from images;

 id  | name    | owner | tags
-----+---------+-------+-----------------
 785 | cat.jpg |  alok | {'cute', 'pet'}

(1 rows)

Adding one or multiple elements
cqlsh:payroll> UPDATE images SET tags = tags + { 'gray', 'cuddly' } 
WHERE id= 785;
cqlsh:payroll> Select * from images;

 id  | name    | owner | tags
-----+---------+-------+-----------------------------------
 785 | cat.jpg |  alok | {'cuddly', 'cute', 'gray', 'pet'}

(1 rows)

Removing one or multiple elements
cqlsh:payroll> UPDATE images SET tags = tags - { 'cute' } WHERE id= 785;
cqlsh:payroll> select * from images;

 id  | name    | owner | tags
-----+---------+-------+---------------------------
 785 | cat.jpg |  alok | {'cuddly', 'gray', 'pet'}

(1 rows)

3) Lists Collections Data Type

Government Jobs