Cassandra 安装就不说了,网上有很多。
运行cqlsh.bat,
查询keyspace,
cqlsh> describe keyspaces;
创建keyspace
CREATE KEYSPACE IF NOT EXISTS myCas WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};
| 名称 | 类型 | 强制性 | 默认值 | 描述 |
|---|---|---|---|---|
replication | map | yes | 复制策略和用于键空间的选项 | |
durable_writes | simple | no | true | 是否使用提交日志来更新此键空间 |
复制属性是必需的,并且必须至少包含定义要使用的复制策略类的“class”子选项。 其余的子选项取决于使用的复制策略。 默认情况下,Cassandra支持以下'class':
- 'SimpleStrategy':定义整个集群的复制因子的简单策略。 支持的唯一子选项是'replication_factor'以定义该复制因子并且是必需的。
- 'NetworkTopologyStrategy':允许为每个数据中心独立设置复制因素的复制策略。 其余的子选项是键值对,其中键是数据中心名称,其值是关联的复制因子。
删除keyspace
DROP KEYSPACE myCas;
使用keyspace
cqlsh> use mycas;
cqlsh:mycas>创建表:
//创建表
cqlsh:mycas> CREATE TABLE user(
id int,
user_name varchar,
PRIMARY KEY (id) );
查看表:
cqlsh:mycas> describe tables;
user插入数据:
//向表中添加数据
cqlsh:mycas> INSERT INTO users (id,user_name) VALUES (1,'zhangsan');
cqlsh:mycas> select * from users;
id | user_name
----+-----------
1 | zhangsan//从表中查询数据
cqlsh:mycas> select * from users where id=1;
id | user_name
----+-----------
1 | zhangsan
(1 rows)
cqlsh:mycas> select * from users where user_name='zhangsan';
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided o
perators: "
可以看出查询主键可以,但是查询没有索引的user_name却无法查询.
我们创建一个索引后再次尝试:
//创建索引
cqlsh:mycas> create index on user (user_name);
cqlsh:mycas> select * from user where user_name='zhangsan';
id | user_name
----+-----------
1 | zhangsan
试一下更新:
//更新表中数据
cqlsh:mycas> update users set user_name='lisi';
SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:33 mismatched input ';' expecting K
_WHERE">
cqlsh:mycas> update users set user_name='lisi' where id=1;
cqlsh:mycas> select * from users;
id | user_name
----+-----------
1 | lisi可以看出只能按照条件进行更新.
试一下删除:
//删除表中数据
SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:17 mismatched input ';' expecting K
_WHERE">
cqlsh:mycas> delete from users where id=1;
cqlsh:mycas> select * from users;
id | user_name
----+-----------
(0 rows)可以看出删除也只能按照条件进行删除.
1万+

被折叠的 条评论
为什么被折叠?



