cassandra關於集合類型的操作

Cassandra的幾種集合類型(list、set、map)增強了數據庫的表現力,這幾種數據類型用的很方便。我簡單總結了一下這幾種類型的使用方法。
社會我二哥,人狠話不多,直奔主題

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

插入使用下面的形式
insert into test(a,b,c,d) values(1,['listtext1','listtext2'],{'settext1','settext2'},{'mapkey1':'mapvale2','mapkey2':'mapvalue2'});  

第一: list類型
增加元素:
update test set b=b+[‘listtext3′,’listext4’] where a=1;
刪除第i個元素:
你可以使用
delete b[i] from test where a=1;
或者 update test set b[i]=null where a=1;
注:後者的方法是可行的,不過官方文檔沒有說明
刪除內容爲listtext1和listtext2的元素
update test set b = b-[‘listtext1′,’listtext2’] where a=1;

第二: Set類型
增加元素
update test set c=c+{‘settext3′,’settext4’} where a=1;
刪除元素
update test set c=c-{‘settext1′,’settext2’} where a=1;

第三:Map類型
增加元素
update test set d[‘mapkey3′] =’mapvalue3’ where a=1;
或者 update test set d=d+{‘mapkey3′:’mapvalue3′,’mapkey4′:’mapvalue4’} where
a=1;
注:後者的方法是可行的,不過官方文檔沒有說明
刪除元素
delete d[‘mapkey3’] from test where a=1;
或者 update test set d[‘mapkey3’]=null where a=1;
注:後者的方法是可行的,不過官方文檔沒有說明

cassandra中集合類型不能做主鍵,不能建索引。
這種集合類型做爲查詢條件很弱,只有map可以使用元素作爲查詢條件,而且必須加ALLOW FILTERING
select * from test where a=1 and d['mapkey1']='mapvale2' ALLOW FILTERING;




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章