Cassandra集合
Cassandra集合用於處理任務。 您可以在集合中存儲多個元素。 Cassandra支持三種類型的集合:
- Set
- List
- Map
Set集合
Set集合存儲查詢時返回排序元素的元素組。
語法:
Create table table_name
(
id int,
Name text,
Email set<text>,
Primary key(id)
);
示例:
下面舉個例子來展示Set
集合。創建一個具有三列(id
, name
和 email
)的表「employee
」。
use yiibai_ks;
create table employee(
id int,
name text,
email set<text>,
primary key(id)
);
執行上面語句創建表以後如下:
cqlsh:yiibai_ks> create table employee(
... id int,
... name text,
... email set<text>,
... primary key(id)
... );
cqlsh:yiibai_ks> describe employee;
CREATE TABLE yiibai_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:yiibai_ks>
現在,向上面創建的employee
表中插入一些值:
INSERT INTO employee (id, email, name)
VALUES(1, {'[email protected]'}, 'yestouu');
INSERT INTO employee (id, email, name)
VALUES(2,{'[email protected]'}, 'Kanchan');
INSERT INTO employee (id, email, name)
VALUES(3, {'[email protected]'}, 'Maxsu');
執行上面語句結果如下 -
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name)
... VALUES(1, {'[email protected]'}, 'yestouu');
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name)
... VALUES(2,{'[email protected]'}, 'Kanchan');
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name)
... VALUES(3, {'[email protected]'}, 'Maxsu');
cqlsh:yiibai_ks> select * from employee;
id | email | name
----+-----------------------+---------
1 | {'[email protected]'} | yestouu
2 | {'[email protected]'} | Kanchan
3 | {'[email protected]'} | Maxsu
(3 rows)
cqlsh:yiibai_ks>
List集合
當元素的順序重要時,使用列表(List)集合。我們以擴展上面示例中的employee
表並新增一列department
爲例。
alter table employee add department list<text>;
執行結果如下 -
cqlsh:yiibai_ks> alter table employee add department list<text>;
cqlsh:yiibai_ks> select * from employee;
id | department | email | name
----+------------+-----------------------+---------
1 | null | {'[email protected]'} | yestouu
2 | null | {'[email protected]'} | Kanchan
3 | null | {'[email protected]'} | Maxsu
(3 rows)
cqlsh:yiibai_ks>
現在添加新列(department
)。 在新列「department
」中插入一些值。
INSERT INTO employee (id, email, name, department) VALUES(4, {'[email protected]'}, 'Sweetsu', ['IT Devopment']);
執行上面語句結果如下 -
cqlsh:yiibai_ks> alter table employee add department list<text>;
cqlsh:yiibai_ks> select * from employee;
id | department | email | name
----+------------+-----------------------+---------
1 | null | {'[email protected]'} | yestouu
2 | null | {'[email protected]'} | Kanchan
3 | null | {'[email protected]'} | Maxsu
(3 rows)
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name, department) VALUES(4, {'[email protected]'}, 'Sweetsu', ['IT Devopment']);
cqlsh:yiibai_ks> select * from employee;
id | department | email | name
----+------------------+-----------------------+---------
1 | null | {'[email protected]'} | yestouu
2 | null | {'[email protected]'} | Kanchan
4 | ['IT Devopment'] | {'[email protected]'} | Sweetsu
3 | null | {'[email protected]'} | Maxsu
(4 rows)
cqlsh:yiibai_ks>
Map集合
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:yiibai_ks> create table course(
... id int,
... prereq map<text, text>,
... primary key(id)
... );
cqlsh:yiibai_ks> INSERT into course(id,prereq) VALUES(1, {'Programming':'CPP&&Java', 'Network':'Artificail Intelligence'});
cqlsh:yiibai_ks> select * from course;
id | prereq
----+--------------------------------------------------------------------
1 | {'Network': 'Artificail Intelligence', 'Programming': 'CPP&&Java'}
(1 rows)
cqlsh:yiibai_ks>