1,官方文档,基本类型
数据查询语言文档:
cql 支持的数据类型:
相对于 MySQL,有几个类型比较有意思,uuid类型,map,list,set类型,这个优化关联查询,直接将List存入一条记录。
CQL Type | Constants | Description |
---|---|---|
ascii | strings | US-ASCII character string |
bigint | integers | 64-bit signed long |
blob | blobs | Arbitrary bytes (no validation), expressed as hexadecimal |
boolean | booleans | true or false |
counter | integers | Distributed counter value (64-bit long) |
decimal | integers, floats | Variable-precision decimal Java type |
double | integers | 64-bit IEEE-754 floating point Java type |
float | integers, floats | 32-bit IEEE-754 floating point Java type |
inet | strings | IP address string in IPv4 or IPv6 format, used by the python-cql driver and CQL native protocols |
int | integers | 32-bit signed integer |
list | n/a | A collection of one or more ordered elements |
map | n/a | A JSON-style array of literals: { literal : literal, literal : literal ... } |
set | n/a | A collection of one or more elements |
text | strings | UTF-8 encoded string |
timestamp | integers, strings | Date plus time, encoded as 8 bytes since epoch |
timeuuid | uuids | Type 1 UUID only |
tuple | n/a | Cassandra 2.1 and later. A group of 2-3 fields. |
uuid | uuids | A UUID in standard UUID format |
varchar | strings | UTF-8 encoded string |
varint | integers | Arbitrary-precision integer Java type |
java支持的数据类型:
CQL type | Java type |
---|---|
decimal | java.math.BigDecimal |
float | java.lang.Float |
double | java.lang.Double |
varint | java.math.BigInteger |
2,查看,创建keyspace,数据表
查看命令和MySQL类似。
desc cluster;
desc keyspaces;
desc keyspace portfoliodemo;
desc tables;
desc table stocks;
创建keyspace: 默认制定SimpleStrategy的副本类型。
Create a keyspace. cqlsh> CREATE KEYSPACE demodb WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 } AND durable_writes = true; Use the keyspace. cqlsh> USE demodb;创建数据表:
CREATE TABLE users ( userid uuid PRIMARY KEY, first_name text, last_name text, emails set<text>, top_scores list<int>, todo map<timestamp, text>, create_time timestamp );
要按照用户才创建时间倒叙查询,必须再创建表的时候就写好。
CREATE TABLE users ( userid uuid PRIMARY KEY, first_name text, last_name text, emails set<text>, top_scores list<int>, todo map<timestamp, text>, create_time timestamp PRIMARY KEY (userid, create_time) ) WITH CLUSTERING ORDER BY (create_time DESC);
默认定义的时正序,倒叙需要再定义下,并且把这个字段放入到primary key 里面。
更新表结构和mysql类似:
ALTER TABLE users ALTER bio TYPE text;
3,插入数据,更新
和mysq 类似:其中emails是set类型。
INSERT INTO users (userid, first_name, last_name, emails) VALUES(cfd66ccc-d857-4e90-b1e5-df98a3d40cd6 , 'Frodo', 'Baggins', {'f@baggins.com', 'baggins@gmail.com'});
增加emails数据:使用+ UPDATE users SET emails = emails + {'fb@friendsofmordor.org'} WHERE userid = cfd66ccc-d857-4e90-b1e5-df98a3d40cd6; 删除emails数据:使用- UPDATE users SET emails = emails - {'fb@friendsofmordor.org'} WHERE userid = cfd66ccc-d857-4e90-b1e5-df98a3d40cd6; 清空emails数据:使用{} UPDATE users SET emails = {} WHERE userid = cfd66ccc-d857-4e90-b1e5-df98a3d40cd6;
4,查询数据
查询数量 SELECT COUNT(*) FROM users; 查询前10条 SELECT * FROM users LIMIT 10 ALLOW FILTERING; 按照token查询 SELECT * FROM users WHERE TOKEN(userid) >= TOKEN(cfd66ccc-d857-4e90-b1e5-df98a3d40cd6); 查询token内容,token只能是primary key。 SELECT TOKEN(userid) FROM users WHERE TOKEN(userid) >= TOKEN(cfd66ccc-d857-4e90-b1e5-df98a3d40cd6);
还支持distinct,in等查询,但不支持关联查询,毕竟不是关系型数据库。