Cassandra--CQL
本篇博客主要讲Cassandra的基本操作,主要是通过cqlsh
客户端命令。这个也是我们常用的基本操作,后面再会讲解一下通过java api的方式操作Cassandra。
下面让我们一起来熟悉吧!
启动cqlsh
启动cqlsh
,如果rpc_address不是设置为0.0.0.0,直接输入cqlsh是会拒绝连接的
Connection error: (‘Unable to connect to any servers’, {‘127.0.0.1’: error(111, “Tried connecting to [(‘127.0.0.1’, 9042)]. Last error: Connection refused”)})需要指定ip,具体操作如下图(我们上篇博客设置为节点的ip)
Keyspace键空间操作
查看所有keyspace
- 命令:
describe keyspaces 或者 desc keyspaces
;
系统默认了五个键空间:system_traces system_schema system_auth system system_distributed
查看某个keyspace
- 命令:
describe keyspace keyspacesName 或者 desc keyspace keyspacesName
创建keyspace
注意:cassandra关键字不区分大小写
1
2cqlsh> CREATE KEYSPACE IF NOT EXISTS testSpace
... WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};
修改删除keyspace
- 命令:
alter keyspace keyspaceName with replication ...
删除:
drop keyspace keyspaceName
1
2
3cqlsh> alter keyspace testspace
... with replication={'class': 'SimpleStrategy', 'replication_factor':2};
cqlsh> drop keyspace testspace;
Table
创建表
注意:table必须定义一个PRIMARY KEY。一个PRIMARY KEY可以由一个或多个columns组成
1
2
3
4
5
6
7
8
9
10
11
12
13cqlsh> use testSpace;
cqlsh:testspace> create table table1(
... id int primary key,
... name text
... );
## 也可以写出
cqlsh> use testSpace;
cqlsh:testspace> create table table1(
... id int ,
... name text ,
... primary key(id) ,
... );
查看表结构
- desc或者describe都行:
desc table tableName
修改表
修改表的属性:
ALTER TABLE <tableName> WITH Properties;
增加表的列:
ALTER TABLE <tableName> ADD columnName columnType;
增加表的列:
ALTER TABLE <tableName> DROP columnName;
插入表数据
- 和sql语句相同:
insert into table(column,...) values(value,...)
删除表数据
- 命令:
delete from table where ...
,必须要有where,例如1
delete from table1 where id = 1;
清空表数据:
- 命令:
TRUNCATE table
,RUNCATE语句将会完全删除所有table的数据