Updating a List in Cassandra

In Cassandra list is a Collection Data Type. In this article, we will discuss how we can update a list in different ways such that we can add elements in the list at any position where we want. we can also append a list.

Let’s discuss one by one.

Consider cluster1 is a keyspace.

CREATE KEYSPACE cluster1
WITH replication = {'class': 'SimpleStrategy', 
                    'replication_factor' : 1}; 

Now, here we are going to create a table first. let’s have a look.

Create Table Food_App 
(
 Cafe_id Int Primary Key,
  Order_Date Date,
  Order_status Text,    
  Cafe_items list<text>
 );  

Now, we are going to insert some data into the Food_App table.



Let’s have a look.

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8045, '2019-02-13', 'Available', {'Banana', 'Mango', 'Apple'});

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8046, '2019-02-15', 'Not Available', {'guava', 'juice', 'milk'});

INSERT INTO Food_App (Cafe_id, Order_Date, Order_status, Cafe_items)                                         
VALUES (8047, '2019-02-18', 'Available', {'grapes', 'papaya', 'mix fruit'}); 

Now, let’s see the output.

select * 
from Food_App; 

Output:

Updating a list:

UPDATE Food_App
SET Cafe_items = ['fruits', 'lemon tea', 'green tea']
Where Cafe_id = 8045; 

Now, let’s see the output.

select * 
from Food_App; 

Output:



To append an element to the list, switch the order of the new element data and the list name:

UPDATE Food_App
SET Cafe_items = Cafe_items + ['bread'] 
WHERE Cafe_id = 8045; 

Now, let’s see the output.

select * 
from Food_App; 

Output:

To prepend an element to the list, enclose it in square brackets and use the addition (+) operator:

UPDATE Food_App
SET Cafe_items = ['bread']+Cafe_items   
WHERE Cafe_id = 8046; 

Now, let’s see the output.

select * 
from Food_App; 

Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced 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.