1 操作键空间

1.1 创建Keyspace

语法
CREATE KEYSPACE <identifier> WITH <properties>;
更具体的语法:
Create keyspace KeyspaceName with replicaton={'class':strategy name,'replication_factor': No of replications on different nodes};

要填写的内容:
KeyspaceName 代表键空间的名字
strategy name 代表副本放置策略,内容包括:简单策略、网络拓扑策略,选择其中的一个。
No of replications on different nodes 代表复制因子,放置在不同节点上的数据的副本数。

编写完成的创建语句 创建一个键空间名字为:school,副本策略选择:简单策略 SimpleStrategy,副本因子:3

create keyspace school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};

Cassandra基本操作(键空间、表、索引)_数据类型

输入DESCRIBE keyspaces查看所有的键空间

Cassandra基本操作(键空间、表、索引)_数据类型_02

输入DESCRIBE school 查看键空间的创建语句,代码:

Cassandra基本操作(键空间、表、索引)_ide_03

1.2连接Keyspace

语法 
USE <identifier>;

编写完整的连接Keyspace语句,连接school 键空间
use school;

1.3修改键空间

语法
ALTER KEYSPACE <identifier> WITH <properties>

编写完整的修改键空间语句,修改school键空间,把副本引子 从3 改为1
alter keyspace school WITH replication = {'class':'SimpleStrategy', 'replication_factor': 1};

Cassandra基本操作(键空间、表、索引)_数据类型_04

1.4 删除键空间

语法
DROP KEYSPACE <identifier>

完整删除键空间语句,删除school键空间
代码drop keyspace school;

2 操作表、索引

注意:操作前,先把键空间school键空间创建,并使用school 键空间
代码:
create keyspace school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
use school;

2.1 查看键空间下所有表

代码
DESCRIBE TABLES;

当前键空间下没有任何表,效果
执行返回empty

Cassandra基本操作(键空间、表、索引)_ide_05

2.2 创建表

语法
CREATE (TABLE | COLUMNFAMILY) <tablename> ('<column-definition>' , '<column-definition>')
(WITH <option> AND <option>)

完整创建表语句
创建student 表,student包含属性如下: 
学生编号(id), 
姓名(name),
年龄(age),
性别(gender),
家庭地址(address),
interest(兴趣),
phone(电话号码),
education(教育经历) 
id 为主键,并且为每个Column选择对应的数据类型。 
注意:interest 的数据类型是set ,phone的数据类型是list,education 的数据类型是map
create table student(
  id int primary key,
  name text,
  age int,
  gender tinyint,
  address text,
  interest set<text>,
  phone list<text>,
  education map<text, text>
);

使用DESCRIBE TABLE student; 查看创建的表

Cassandra基本操作(键空间、表、索引)_Cassandra_06

2.3 cassandra的索引(KEY)

上面创建student的时候,把student_id 设置为primary key 在Cassandra中的primary key是比较宏观概念,用于从表中取出数据。primary key可以由1个或多个column组合而成。 不要在以下情况使用索引:

  • 这列的值很多的情况下,因为你相当于查询了一个很多条记录,得到一个很小的结果。
  • 表中有couter类型的列
  • 频繁更新和删除的列
  • 在一个很大的分区中去查询一条记录的时候(也就是不指定分区主键的查询)

Cassandra的5种Key

  • Primary Key
  • Partition Key
  • Composite Key
  • Compound Key
  • Clustering Key

2.3.1 Primary Key

是用来获取某一行的数据, 可以是单一列(Single column Primary Key)或者多列(Composite Primary Key)。
在 Single column Primary Key 决定这一条记录放在哪个节点。

create table test01(
id int primary key,
name text
);
insert into test01(id,name) values(1,'zhangsan');

Cassandra基本操作(键空间、表、索引)_ide_07

2.3.2 Composite Primary Key

如果Primary Key 由多列组成,那么这种情况称为 Compound Primary Key 或 Composite Primary Key。

例如:
create table test02(
key_one int,
key_two int,
name text,
primary key(key_one,key_two)
);
执行创建表后,查询test02,会发现key_one和key_two 的颜色与其他列不一样

Cassandra基本操作(键空间、表、索引)_Cassandra_08

2.3.3 Partition Key

在组合主键的情况下(上面的例子),第一部分称作Partition Key(key_one就是partition key),第二部分是CLUSTERING KEY(key_two)

Cassandra会对Partition key 做一个hash计算,并自己决定将这一条记录放在哪个节点。

如果 Partition key 由多个字段组成,称之为 Composite Partition key
例如:
create table test03(
key_part_one int,
key_part_two int,
key_clust_one int,
key_clust_two int,
key_clust_three uuid,
name text,
primary key((key_part_one,key_part_two),key_clust_one,key_clust_two,key_clust_three)
);

Cassandra基本操作(键空间、表、索引)_数据类型_09

2.3.4 Clustering Key

决定同一个分区内相同Partition Key数据的排序,默认为升序,可以在建表语句里面手动设置排序的方式

2.4 修改表结构

语法,可以添加列,删除列

2.4.1 添加列

语法
ALTER TABLE table name ADD  new column datatype;

给student添加一个列email代码:
alter table student add email text;

2.4.2 删除列

语法
ALTER table name DROP columnname;

alter table student drop email;

2.5 删除表

语法
DROP TABLE <tablename>

删除student,代码如下:
drop table student;

2.6 清空表

表的所有行都将永久删除
语法
TRUNCATE <tablename>

代码
TRUNCATE table test01;

2.7 创建索引

2.7.1 普通列创建索引

语法
CREATE INDEX <identifier> ON <tablename>

为student的 name 添加索引,索引的名字为:sname, 代码:
create index sname on student(name);

为student 的age添加索引,不设置索引名字,代码

Cassandra基本操作(键空间、表、索引)_ide_10

可以发现 对age创建索引,没有指定索引名字,会提供一个默认的索引名:student_age_idx。

索引原理:

Cassandra之中的索引的实现相对MySQL的索引来说就要简单粗暴很多了。Cassandra自动新创建了一张表格,同时将原始表格之中的索引字段作为新索引表的Primary Key!并且存储的值为原始数据的Primary Key

2.7.2 集合列创建索引

给集合列设置索引
CREATE INDEX ON student(interest);                 -- set集合添加索引
CREATE INDEX mymap ON student(KEYS(education));          -- map结合添加索引

Cassandra基本操作(键空间、表、索引)_数据类型_11

2.8 删除索引

语法
DROP INDEX <identifier>

删除student的sname 索引,代码
drop index sname;

联想
如果不加表名就删除索引,那么至少在整个keyspace里面,索引名必须是唯一的
尝试创建同名索引

Cassandra基本操作(键空间、表、索引)_Cassandra_12