cassandra operations on collection types

Example:

create table test(
	a int,
	b list<text>,
	c set<text>,
	d map<text,text>,
	primary key(a)
);

 Insert using the following form

insert into test(a,b,c,d) values(1,[‘listtext1′,’listtext2’],{‘settext1′,’settext2’},{‘mapkey1′:’mapvale2′,’mapkey2′:’mapvalue2’});

First:
add elements to the list type:
update test set b=b+['listtext3','listext4'] where a=1;

Delete the i-th element:
you can use
delete b[i] from test where a=1;
or update test set b[i]=null where a=1;
Note: The latter method is feasible, but the official documentation does not illustrate

Delete elements with listtext1 and listtext2
update test set b = b-['listtext1','listtext2'] where a=1;

Second: Set type
add element
update test set c=c+{'settext3','settext4'} where a=1;
delete element
update test set c=c-{'settext1','settext2'} where a=1;

Third:
add elements to Map type
update test set d['mapkey3'] ='mapvalue3' where a=1;
or update test set d=d+{'mapkey3':'mapvalue3','mapkey4':'mapvalue4'} where
a=1;
Note: The latter method is feasible, but the official documentation does not state it

Delete the element
delete d['mapkey3'] from test where a=1;
or update test set d['mapkey3']=null where a=1;
Note: The latter method is feasible, but the official documentation does not state it

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326901221&siteId=291194637