cassandra关于集合类型的操作

来源:互联网 发布:ug侧铣头编程 编辑:程序博客网 时间:2024/05/22 09:03
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;




原创粉丝点击