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, {'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:yiibai_ks> INSERT INTO employee (id, email, name)
             ... VALUES(1, {'yestouu@gmail.com'}, 'yestouu');
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name)
             ... VALUES(2,{'kanchan@qq.com'}, 'Kanchan');
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name)
             ... VALUES(3, {'maxsu@126.com'}, 'Maxsu');
cqlsh:yiibai_ks> select * from employee;

 id | email                 | name
----+-----------------------+---------
  1 | {'yestouu@gmail.com'} | yestouu
  2 |    {'kanchan@qq.com'} | Kanchan
  3 |     {'maxsu@126.com'} |   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 | {'yestouu@gmail.com'} | yestouu
  2 |       null |    {'kanchan@qq.com'} | Kanchan
  3 |       null |     {'maxsu@126.com'} |   Maxsu

(3 rows)
cqlsh:yiibai_ks>

现在添加新列(department)。 在新列“department”中插入一些值。

INSERT INTO employee (id, email, name, department) VALUES(4, {'sweetsu@gmail.com'}, '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 | {'yestouu@gmail.com'} | yestouu
  2 |       null |    {'kanchan@qq.com'} | Kanchan
  3 |       null |     {'maxsu@126.com'} |   Maxsu

(3 rows)
cqlsh:yiibai_ks> INSERT INTO employee (id, email, name, department) VALUES(4, {'sweetsu@gmail.com'}, 'Sweetsu', ['IT Devopment']);
cqlsh:yiibai_ks> select * from employee;

 id | department       | email                 | name
----+------------------+-----------------------+---------
  1 |             null | {'yestouu@gmail.com'} | yestouu
  2 |             null |    {'kanchan@qq.com'} | Kanchan
  4 | ['IT Devopment'] | {'sweetsu@gmail.com'} | Sweetsu
  3 |             null |     {'maxsu@126.com'} |   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>

        原文作者:Cassandra教程
        原文地址: https://www.yiibai.com/cassandra/cassandra-collections.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞