Cassandra集合


Cassandra集合用於處理任務。 您可以在集合中儲存多個元素。 Cassandra支援三種型別的集合:

  • Set
  • List
  • Map

Set集合

Set集合儲存查詢時返回排序元素的元素組。

語法:

Create table table_name  
(  
    id int,  
    Name text,  
    Email set<text>,  
    Primary key(id)  
);

範例:

下面舉個例子來展示Set集合。建立一個具有三列(id, nameemail)的表「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>