Open In App

Updating MAP collection data type in Cassandra

Improve
Improve
Like Article
Like
Save
Share
Report

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 Curly 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.


Last Updated : 29 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads