Tuesday, 3 March 2020

Cassandra: Working with maps

Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.

Roll Number
Name
123
Krishna
124
Ram
125
Anil
126
Kiran

In the above table, Roll number is the key, which maps to student name. I.e, Roll Number 123 maps to the student name Krishna, 126 maps to the student name Kiran.

Map in Cassandra always sorted by the keys.

How to create column of type map?
Using ‘map’ keyword, you can create a column of type map.

CREATE TABLE IF NOT EXISTS cassandratutorial.organization (
  id INT PRIMARY KEY,
  organization_name VARCHAR,
  employee_names MAP<INT, VARCHAR>
);

How to insert data to a map?
Using JSON kind of syntax, you can insert data to a map.

INSERT INTO cassandratutorial.organization (id, organization_name, employee_names) VALUES (1, 'ABC Corporation', {1 : 'Krishna', 5 : 'Ram', 3 : 'Chamu', 4 : 'Sowmya'});
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.organization (
   ...   id INT PRIMARY KEY, 
   ...   organization_name VARCHAR,
   ...   employee_names MAP<INT, VARCHAR>
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.organization (id, organization_name, employee_names) VALUES (1, 'ABC Corporation', {1 : 'Krishna', 5 : 'Ram', 3 : 'Chamu', 4 : 'Sowmya'});
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.organization;

 id | employee_names                                    | organization_name
----+---------------------------------------------------+-------------------
  1 | {1: 'Krishna', 3: 'Chamu', 4: 'Sowmya', 5: 'Ram'} |   ABC Corporation

(1 rows)

Updating key-value pair
Using indexing notation, you can update key-value pair in the map.

UPDATE cassandratutorial.organization SET employee_names[1] = 'Hari Krishna Gurram' WHERE id = 1;
cqlsh> UPDATE cassandratutorial.organization SET employee_names[1] = 'Hari Krishna Gurram' WHERE id = 1;
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.organization;

 id | employee_names                                                | organization_name
----+---------------------------------------------------------------+-------------------
  1 | {1: 'Hari Krishna Gurram', 3: 'Chamu', 4: 'Sowmya', 5: 'Ram'} |   ABC Corporation

(1 rows)

Adding new key-value pair to the map
Using + operator, you can add new key-value pair to the map.

UPDATE cassandratutorial.organization SET employee_names = employee_names +  { 2 : 'Lahari' } WHERE id = 1;
cqlsh> UPDATE cassandratutorial.organization SET employee_names = employee_names +  { 2 : 'Lahari' } WHERE id = 1;
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.organization;

 id | employee_names                                                             | organization_name
----+----------------------------------------------------------------------------+-------------------
  1 | {1: 'Hari Krishna Gurram', 2: 'Lahari', 3: 'Chamu', 4: 'Sowmya', 5: 'Ram'} |   ABC Corporation

(1 rows)

Deleting a key-value pair
Using DELETE query, you can delete a key-value pair from map.

DELETE employee_names[2] FROM cassandratutorial.organization WHERE id = 1;
cqlsh> DELETE employee_names[2] FROM cassandratutorial.organization WHERE id = 1;
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.organization;

 id | employee_names                                                | organization_name
----+---------------------------------------------------------------+-------------------
  1 | {1: 'Hari Krishna Gurram', 3: 'Chamu', 4: 'Sowmya', 5: 'Ram'} |   ABC Corporation

(1 rows)

Setting TTL (time to live) property to a key-value pair
You can set TTL in both INSERT and UPDATE statements.

UPDATE cassandratutorial.organization USING TTL 100 SET employee_names[1] = 'Sailaja' WHERE id = 1;
cqlsh> UPDATE cassandratutorial.organization USING TTL 100 SET employee_names[1] = 'Sailaja' WHERE id = 1;
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.organization;

 id | employee_names                                    | organization_name
----+---------------------------------------------------+-------------------
  1 | {1: 'Sailaja', 3: 'Chamu', 4: 'Sowmya', 5: 'Ram'} |   ABC Corporation

(1 rows)

In the above example I set 100 seconds time for ‘employee_names[1]’.


Previous                                                    Next                                                    Home

No comments:

Post a Comment