Updating MAP collection data type in Cassandra

In this article, we will discuss how we can update the MAP Collection Data Type and how we can update rows, how we can remove rows and how we can add rows by using the UPDATE clause for updating the map collection data types.

First, we will create a table let’s consider Food_menu is a table name and Order_id, Order_Date, Oreder_cost, and Menu_items are the fields. let’s have a look.

Create Table Food_menu (
  Order_id Int Primary Key,
  Order_Date Date,
  Order_cost int,    
  Menu_items MAP<text, text>
 ); 

Now, Insert some data into the table by using the following CQL query given below.

INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items)
VALUES (2501, '2019-02-13', 800, {'fruit' : 'banana',  'juice' : 'Apple juice'});

INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items)
VALUES (2502, '2019-04-23', 200, {'fruit' : 'mango',  'juice' : 'mango juice'});

INSERT INTO Food_menu (Order_id, Order_Date, Order_cost, Menu_items)
VALUES (2503, '2019-02-13', 600, {'fruit' : 'papaya',  'juice' : 'papaya juice'}); 

Now, let’s see the output of the above CQL query.

select * 
from Food_menu; 

Output:



Now, here we will see how Updating a MAP and we can append MAP collection data type by using (+) operator and whatever the elements we want to add then strings in Curley brackets in key pair value.
Let’s have a look.

UPDATE Food_menu
SET Menu_items = Menu_items + {'Extra 10' : 'biscuits'} 
WHERE Order_id = 2501; 

Let’s see the output,

select * 
from Food_menu;

Output:

Now, here we will see how we can update the MAP collection data type by using the TTL value.

UPDATE Food_menu
USING TTL 259200
SET Menu_items['beverage'] = 'coffee' 
WHERE Order_id = 2502; 

Let’s see the output,
Output:

In CQL we can add one or more elements that are separated by a comma for updating the map. let’s have a look.
For example, This is also one of a straightforward method that is supported by CQL for creating a new row by using the UPDATE clause which contains a collection map.

UPDATE Food_menu
SET Menu_items = Menu_items + {'Dry_fruit' : 'Almonds', 'cookies' : 'biscuits'} 
WHERE Order_id = 2503; 

Let’s see the output,

select * 
from Food_menu; 

Output:

In Cassandra Query Language (CQL), when we deleting entries in updating collection data types like set and map then we are replacing elements or key-value pairs with a new collection which creates tombstones for the deleted entries even though we know they will be entries in the new map collection or set. Suppose if your code updates all map and set collections these ways then it generates many tombstones that can affect the system performance more specifically we can say may slow the system down.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.