天天看点

Cassandra 最佳实践系列(1) - CQL 基本数据类型

Cassandra最佳实践细节(1)常见CQL使用(1)

数据类型

CQL_TYPE包含如下几类:

  • native type;
  • collection type;
  • user defined type;
  • tuple type;
  • Custom type;

native type

类型 变量可用类型 描述
ascii string ASCII 字符串
bigint integer 64位有符号long
blob 任意字节
boolean true、false
counter counter 列(64位有符号)
date integer、string 日期常量类型
decimal integer、float 可变精度小数
double 64位IEEE-754浮点
duration duratio 纳秒精确度的持续时间
float 32位IEEE-754浮点
inet IP地址、ipv4或者ipv6,因为没有ip类型,所以用string作为输入
int 32位 有符号int
smallint 16位有符号int
text UTF-8编码string
time 纳秒精度的时间
timestamp 毫秒时间戳
timeuuid uuid
tinyint 8位有符号int
varchar UTF8string
varint 任意精度证书

使用counter

可以用来定义counter 列,这个列支持2种操作:incrementing 以及decrementing,使用限制是:

  • primary key不可以包含counter 列;
  • 一个表如果包含了counter,那么就只能包含counter,换句话说就是:如果primary key外的列要么全是counter,要么就没有;
  • counter列不支持过期;
  • counter列支持delete,但是只能支持delete一次;
  • counter理论上是不是很理想的,如果超时等可能影响准确性。

使用timestamp

timestamp 类型是64位的有符号的int,代表的是从1970年00:00:00以来的毫秒时间戳。在CQL里面timestamp可以使用integer以及string表示,比如2011年3月21号 04:05:00 AM, GMT;

  • 1299038700000

  • '2011-02-03 04:05+0000'

  • '2011-02-03 04:05:00+0000'

  • '2011-02-03 04:05:00.000+0000'

  • '2011-02-03T04:05+0000'

  • '2011-02-03T04:05:00+0000'

  • '2011-02-03T04:05:00.000+0000'

使用date

date类型的value 可以是string或者integer的类型,基本的表述格式:yyyy-mm-dd

使用时间类型 times

times的类型value是可以是string或者integer,它的使用格式是:hh:mm:ss[.ffffffff],举个例子,可以如下表示一个时间:

  • '08:12:54'

  • '08:12:54.123'

  • '08:12:54.123456'

  • '08:12:54.123456789'

使用duration

duration类型的数据是由3个有符号的integer组成的,第一个表示的是month,第二个表示的是天,第三个表示的是纳秒。duration的的输入可以如下:

  • 时间单元可以比如:12h30m,相关的时间单元可以如下:
    • y:年(12个months)
    • mo:months(1 month)
    • w: weeks(7 days)
    • d:days (1 day)
    • h:hours(3,600,000,000,000纳秒)
    • m:分钟(60,000,000,000 纳秒)
    • s:秒(1,000,000,000纳秒)
    • ms:毫秒(1000,000 纳秒)
    • us: 微妙(1000纳秒)
    • ns:纳秒(1纳秒)
  • ISO 8601 格式:

    P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W

  • ISO 8601 可选格式:

    P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]

列子:

INSERT INTO RiderResults (rider, race, result) VALUES ('Christopher Froome', 'Tour de France', 89h4m48s);
INSERT INTO RiderResults (rider, race, result) VALUES ('BARDET Romain', 'Tour de France', PT89H8M53S);
INSERT INTO RiderResults (rider, race, result) VALUES ('QUINTANA Nairo', 'Tour de France', P0000-00-00T89:09:09);           

注意 : duration的列无法做为primary key,主要原因是因为在没有date类型保序的情况下无法将duration的相关数据进行排序;

Collection type

Cassandra 支持3种集合类型:Maps、Lists、Sets,对于使用这几个类型有一些需要的建议如下:

  • 对于一些有限的排序/denormalizing 的数据,可以使用collection,但是无限的数据不建议用这个类型存;
  • 单个集合内部元素并没有被索引,所以如果需要读取单个集合内部的某个元素会将整个元素都读取出来;
  • 对于sets以及maps的写操作不会引起read-before-write,但是对于list操作来说可能会存在。所以对于有的情况下的list操作并不建议,反之set之类可能更合适。

maps

map是一个以key排序的k-v对,使用方式如下:

CREATE TABLE users (
    id text PRIMARY KEY,
    name text,
    nametoaddress map<text, text> 
);

INSERT INTO users (id, name, favs)
           VALUES ('id1', 'gcc', { 'xl' : 'hz', 'gc' : 'bj' });

UPDATE users SET nametoaddress = { 'xl' : 'china' } WHERE id = 'id1';           

map类支持:

  • update 或者insert 单个或者多个元素:
UPDATE users SET nametoaddress['gc'] = 'earth' WHERE id = 'id1';
UPDATE users SET nametoaddress = nametoaddress + { 'll' : 'sh', 'nn' : 'sz' } WHERE id = 'id1';           
  • 删除单个或者多个元素:
DELETE nametoaddress['xl'] FROM users WHERE id = 'id1';
UPDATE users SET nametoaddress = nametoaddress - { 'xl', 'll'} WHERE id = 'id1';           
  • 支持TTL操作

sets

set是有序的值排序,类似我们常见的set集合类型:

CREATE TABLE images (
    name text PRIMARY KEY,
    owner text,
    tags set<text>  
);

INSERT INTO images (name, owner, tags)
            VALUES ('cat.jpg', 'gcc', { 'pet', 'cute' });

UPDATE images SET tags = { 'kitten', 'cat', 'lol' } WHERE name = 'cat.jpg';           

set操作支持下面:

  • 给集合添加以及删除一个或者多个元素:
UPDATE images SET tags = tags + { 'gray', 'cuddly' } WHERE name = 'cat.jpg';
UPDATE images SET tags = tags - { 'cat' } WHERE name = 'cat.jpg';           

lists

list和我们常见的理解的list集合类型类似:

CREATE TABLE plays (
    id text PRIMARY KEY,
    game text,
    players int,
    scores list<int>  
)

INSERT INTO plays (id, game, players, scores)
           VALUES ('123-afde', 'quake', 3, [17, 4, 2]);

UPDATE plays SET scores = [ 3, 9, 4] WHERE id = '123-afde';           

list支持下面操作:

  • 将值添加到列表:
UPDATE plays SET players = 5, scores = scores + [ 14, 21 ] WHERE id = '123-afde';
UPDATE plays SET players = 6, scores = [ 3 ] + scores WHERE id = '123-afde';           
  • 设置list中某个特定位置的值:
UPDATE plays SET scores[1] = 7 WHERE id = '123-afde';           
  • 删除某个特定位置上的值:
DELETE scores[1] FROM plays WHERE id = '123-afde';           
  • 删除相关存在的值:
UPDATE plays SET scores = scores - [ 12, 21 ] WHERE id = '123-afde';           

UDT (user-defined-type)

支持用户自定义的类型,这个需要指定特定的keyspace下面进行对应的create 操作,对于udt,支持create、alter、drop等操作。

create udt

一定要在一个具体的keyspace下面使用,参考:

Cassandra 最佳实践系列(1) - CQL 基本数据类型

此外还支持alter以及drop操作。

注意:如果一个udt被一个具体的表使用,如果想drop这个udt,那么需要对应的表也要drop;

tuple

使用参考下面的列子:

Cassandra 最佳实践系列(1) - CQL 基本数据类型