Cassandra集合用于处理任务。 您可以在集合中存储多个元素。 Cassandra支持三种类型的集合:
Set集合存储查询时返回排序元素的元素组。
语法:
Create table table_name ( id int, Name text, Email set<text>, Primary key(id) );
示例:
下面举个例子来展示Set
集合。创建一个具有三列(id
, name
和 email
)的表“employee
”。
use zyiz_ks; create table employee( id int, name text, email set<text>, primary key(id) );
执行上面语句创建表以后如下:
cqlsh:zyiz_ks> create table employee( ... id int, ... name text, ... email set<text>, ... primary key(id) ... ); cqlsh:zyiz_ks> describe employee; CREATE TABLE zyiz_ks.employee ( id int PRIMARY KEY, email set<text>, name text ) WITH bloom_filter_fp_chance = 0.01 AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'} AND comment = '' AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'} AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'} AND crc_check_chance = 1.0 AND dclocal_read_repair_chance = 0.1 AND default_time_to_live = 0 AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = 0 AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry = '99PERCENTILE'; cqlsh:zyiz_ks>
现在,向上面创建的employee
表中插入一些值:
INSERT INTO employee (id, email, name) VALUES(1, {'yestouu@gmail.com'}, 'yestouu'); INSERT INTO employee (id, email, name) VALUES(2,{'kanchan@qq.com'}, 'Kanchan'); INSERT INTO employee (id, email, name) VALUES(3, {'maxsu@126.com'}, 'Maxsu');
执行上面语句结果如下 -
cqlsh:zyiz_ks> INSERT INTO employee (id, email, name) ... VALUES(1, {'yestouu@gmail.com'}, 'yestouu'); cqlsh:zyiz_ks> INSERT INTO employee (id, email, name) ... VALUES(2,{'kanchan@qq.com'}, 'Kanchan'); cqlsh:zyiz_ks> INSERT INTO employee (id, email, name) ... VALUES(3, {'maxsu@126.com'}, 'Maxsu'); cqlsh:zyiz_ks> select * from employee; id | email | name ----+-----------------------+--------- | {'yestouu@gmail.com'} | yestouu | {'kanchan@qq.com'} | Kanchan | {'maxsu@126.com'} | Maxsu (3 rows) cqlsh:zyiz_ks>
当元素的顺序重要时,使用列表(List)集合。我们以扩展上面示例中的employee
表并新增一列department
为例。
alter table employee add department list<text>;
执行结果如下 -
cqlsh:zyiz_ks> alter table employee add department list<text>; cqlsh:zyiz_ks> select * from employee; id | department | email | name ----+------------+-----------------------+--------- | null | {'yestouu@gmail.com'} | yestouu | null | {'kanchan@qq.com'} | Kanchan | null | {'maxsu@126.com'} | Maxsu (3 rows) cqlsh:zyiz_ks>
现在添加新列(department
)。 在新列“department
”中插入一些值。
INSERT INTO employee (id, email, name, department) VALUES(4, {'sweetsu@gmail.com'}, 'Sweetsu', ['IT Devopment']);
执行上面语句结果如下 -
cqlsh:zyiz_ks> alter table employee add department list<text>; cqlsh:zyiz_ks> select * from employee; id | department | email | name ----+------------+-----------------------+--------- | null | {'yestouu@gmail.com'} | yestouu | null | {'kanchan@qq.com'} | Kanchan | null | {'maxsu@126.com'} | Maxsu (3 rows) cqlsh:zyiz_ks> INSERT INTO employee (id, email, name, department) VALUES(4, {'sweetsu@gmail.com'}, 'Sweetsu', ['IT Devopment']); cqlsh:zyiz_ks> select * from employee; id | department | email | name ----+------------------+-----------------------+--------- | null | {'yestouu@gmail.com'} | yestouu | null | {'kanchan@qq.com'} | Kanchan | ['IT Devopment'] | {'sweetsu@gmail.com'} | Sweetsu | null | {'maxsu@126.com'} | Maxsu (4 rows) cqlsh:zyiz_ks>
Map集合用于存储键值对。它将一件事映射到另一件事。 例如,如果要将必备课程名称来保存课程信息,则可以使用Map
集合。
例子:
创建一个名为“course
”的表。
create table course( id int, prereq map<text, text>, primary key(id) ); -- 插入数据 INSERT into course(id,prereq) VALUES(1, {'Programming':'CPP&&Java', 'Network':'Artificail Intelligence'});
现在创建表,在Map
集合类型中插入一些数据。
输出:
cqlsh:zyiz_ks> create table course( ... id int, ... prereq map<text, text>, ... primary key(id) ... ); cqlsh:zyiz_ks> INSERT into course(id,prereq) VALUES(1, {'Programming':'CPP&&Java', 'Network':'Artificail Intelligence'}); cqlsh:zyiz_ks> select * from course; id | prereq ----+-------------------------------------------------------------------- | {'Network': 'Artificail Intelligence', 'Programming': 'CPP&&Java'} (1 rows) cqlsh:zyiz_ks>