java查询cassandra,Cassandra2.1数据查询语法。1,官⽅⽂档,基本类型
数据查询语⾔⽂档:
cql ⽀持的数据类型:
相对于 MySQL,有⼏个类型⽐较有意思,uuid类型,map,list,set类型,这个优化关联查询,直接将List存⼊⼀条记录。
CQL Type
Constantsdistinct查询
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⽀持的数据类型:
查看命令和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,
top_scores list,
todo map,
create_time timestamp
);
Cassandra 有⼀个特性就是底层做好分布式了,所以再查询排序的时候限制就⽐较多。
要按照⽤户才创建时间倒叙查询,必须再创建表的时候就写好。
CREATE TABLE users (
userid uuid PRIMARY KEY,
first_name text,
last_name text,
emails set,
top_scores list,
todo map,
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', 'baggins@gmail'});
更新数据,⽐较特殊的时list,map,set类型:
增加emails数据:使⽤+
UPDATE users SET emails = emails + {''} WHERE userid = cfd66ccc-d857-4e90-b1e5-
df98a3d40cd6;
删除emails数据:使⽤-
UPDATE users SET emails = emails - {''} 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等查询,但不⽀持关联查询,毕竟不是关系型数据库。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论