In this post, I am sharing a demonstration of Cassandra Collection data types.
Basically, CQL provides three different types of collection data types, SET, LIST, MAP.
Using collection data types, you can store multiple values in a single column.
Using collection data types, you can store multiple values in a single column.
You can store multiple values in a single column, but try to keep your collection small otherwise it creates performance overhead.
Collection data types:
Create a sample table with collectio data type
(SET, LIST, MAP):
(SET, LIST, MAP):
1
2
3
4
5
6
7
8
|
CREATE TABLE IF NOT EXISTS tbl_Employee
(
EmpID INT PRIMARY KEY
,EmpName VARCHAR
,Emails SET<text>
,Hobbies LIST<text>
,Address MAP<text,text>
) ;
|
Insert a sample record:
1
2
3
4
5
6
7
8
9
10
|
INSERT INTO tbl_Employee
(EmpID,EmpName,Emails,Hobbies,Address)
VALUES
(
1
,'Anvesh'
,{'test1@gmail.com','test2@gmail.com'}
,['Blogging','Animation','Photography']
,{'home': 'Gujarat', 'office': 'Hyderabad'}
);
|
Update data on Collection type:
1
2
3
|
UPDATE tbl_Employee SET Emails = Emails + {'test3@gmail.com'} WHERE EmpID=1;
UPDATE tbl_Employee SET Hobbies = Hobbies + ['Watching Movies'] WHERE EmpID=1;
UPDATE tbl_Employee SET Address = Address + {'farm':'Bhavnagar'} WHERE EmpID=1;
|
Filter on Collection type:
1
2
3
|
SELECT * FROM tbl_Employee WHERE Address CONTAINS 'Gujarat' ALLOW FILTERING;
SELECT * FROM tbl_Employee WHERE Hobbies CONTAINS 'Blogging' ALLOW FILTERING;
SELECT * FROM tbl_Employee WHERE Emails CONTAINS 'test1@gmail.com' ALLOW FILTERING;
|
0 comments:
Post a Comment