cassandra常用基本操作

1. 登录:
./cqlsh  172.31.0.7      或者
./cqlsh -u cassandra -p cassandra 172.31.0.7
2. 创建用户(用的不多,一般不用)
CREATE USER root WITH PASSWORD 'root' SUPERUSER;
3. 查询所有keyspaces
describe keyspaces;(或desc keyspaces;)
4. 创建一个keyspaces
CREATE KEYSPACE IF NOT EXISTS myCas WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};
5. 使用keyspaces
use myCas;
6. 查询所有table
desc tables;
7. 创建一张表
CREATE TABLE user (id int, user_name varchar, PRIMARY KEY (id) );
创建表的时候至少指定一个主键
8. 向表中插入一条记录
INSERT INTO user (id,user_name) VALUES (1,'zhangsan');
列名必须要显示指定,如果表中已存在相同主键的记录,那么该操作会覆盖表中已存在的记录
9. 查询表中全部数据
select * from user;
cassandra查询有很多限制,比如只能单表查询,不支持联表查询和子查询,查询条件只支持key查询和索引列查询,而且key有顺序的限制
10. 简单的条件查询:
select * from user where id=1;
11. 创建索引:
create index on user(user_name);
12. 索引列查询:
select * from user where user_name='zhangsan';
若没有在name上创建索引,那么此查询会报错;
13. 更新操作
update user set user_name='lisi' where id=2;
只支持按主键更新,也就是where后只能跟主键
14. 删除表中记录
delete from user where id=1;
删除某条记录中的某个字段,该字段会被设成null:
delete user_name from user where id=1;
无论是删除某条记录,还是将某个字段置null,都只支持按主键删除,也就是where后只能跟主键
15. 批量操作:Multiple INSERT, UPDATE and DELETE can be executed in a single statement by grouping them through a BATCH statement(通过batch能够执行多个insert、update以及delete)
BEGIN BATCH
  INSERT INTO user (id, user_name) VALUES (2, 'second user');
    UPDATE user SET user_name = 'ps22dhds' WHERE id = 2;
    INSERT INTO user (id, user_name) VALUES (3, 'lee long');
     DELETE user_name FROM user WHERE id = 3;
APPLY BATCH;

16. 查询系统键空间
SELECT * FROM system.schema_keyspaces;
17. 修改Keyspace
ALTER KEYSPACE "wuzna" WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : '2'};
18. Durable_writes
此选项,可以指示Cassandra是否对当前KeySpace的更新使用commitlog。此选项不是强制性的,默认情况下,它设置为true;
19. 删除键空间
drop keyspace wuzna;
20. 修改表
添加列:ALTER TABLE emp ADD emp_email text;
删除列:ALTER TABLE emp DROP emp_email;

你可能感兴趣的:(cassandra)