Cassandra key说明——Cassandra 整体数据可以理解成一个巨大的嵌套的Map Map<RowKey, SortedMap<ColumnKey, ColumnValue>>
Cassandra之中一共包含下面5种Key:
- Primary Key
- Partition Key
- Composite Key
- Compound Key
- Clustering Key
首先,Primary key 是用来获取某一行的数据, 可以是一列或者多列(复合列 composite)
Primary = Partition Key + [Clustering Key] (Clustering Key 可选)
Clustering keys 包括下面两种情况:
(1) composite key
(2) compound key
1
2
3
4
5
6
7
8
9
10
11
12
|
-- 一列
create table stackoverflow (
key text PRIMARY KEY,
data text
);
-- 复合列
create table stackoverflow (
key_part_one text,
key_part_two int,
data text,
PRIMARY KEY(key_part_one, key_part_two)
);
|
在上面复合列的table之中,全称: Composite Primary Key
并且:
(1) key_part_one –> partition key
(2) key_part_two –> clustering key
注意: partition key, clustering key 都可以是复合列。
Partition Key : Cassandra会对partition key 做一个hash计算,并自己决定将这一条记录放在哪个node
Partition Key的设计,可以完全的借用MySQL的主键。
Cassandra会给每一行数据一个timestamp,如果有多行数据,Cassandra会取时间最新的数据返回!
Clustering Key : 主要用于进行Range Query. 并且使用的时候需要按照建表顺序进行提供信息!
参考下面代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
-- 创建表
-- 注意state 这个field
CREATE TABLE users (
mainland text,
state text,
uid int,
name text,
zip int,
PRIMARY KEY ((mainland), state, uid)
)
-- 插入一些值
insert into users (mainland, state, uid, name, zip)
VALUES ( 'northamerica', 'washington', 1, 'john', 98100);
xxxx more
insert into users (mainland, state, uid, name, zip)
VALUES ( 'southamerica', 'argentina', 6, 'alex', 10840);
|
有效的查询:
1
|
select * from users where mainland = 'northamerica' and state > 'ca' and state < 'ny';
|
本质是先node上查找后,然后range筛选!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」