Cassandra数据模型

1. 概述

Cassandra是一套开源分布式NoSQL数据库系统,设计思想采用了google的BigTable的数据模型和Amazon的Dynamo的完全分布式架构,因而它具有很好的扩展性且不存在单点故障。

本文假设读者已经具有了SQL数据库的基本知识,为了帮助读者更容易的理解Cassandra的数据模型,本文结合SQL数据库的概念,采用类比的方法介绍Cassandra的数据模型。

2. 数据模型

2.1 Key(对应SQL数据库中的主键)

在Cassandra中,每一行数据记录是以key/value的形式存储的,其中key是唯一标识。

2.2 column(对应SQL数据库中的列)

Cassandra中每个key/value对中的value又称为column,它是一个三元组,即:name,value和timestamp,其中name需要是唯一的。

2.3 super column(SQL数据库不支持)

cassandra允许key/value中的value是一个map(key/value_list),即某个column有多个子列。

2.4 Standard Column Family(相对应SQL数据库中的table)

每个CF由一系列row组成,每个row包含一个key以及其对应的若干column。

2.5 Super Column Family(SQL数据库不支持)

每个SCF由一系列row组成,每个row包含一个key以及其对应的若干super column。

注意,CF中只能存储name/value形式的column,不能存储Super column,同样,SCF中只能存储super column。

2.6 Key Space(对应SQL数据库中的database)

一个Key Space中可包含若干个CF,如同SQL数据库中一个database可包含多个table。

2.7 排序

在Cassandra的每个row中,所有Column按照会按照name自动进行排序,排序的类型有:BytesType, UTF8Type,LexicalUUIDType, TimeUUIDType, AsciiType,和LongType,不同的排序类型,会产生不同的排序结果,如:

{name: 123, value: “hello there”},

{name: 832416, value: “kjjkbcjkcbbd”},

{name: 3, value: “101010101010″},

{name: 976, value: “kjjkbcjkcbbd”}

采用LongType排序类型,结果是:

{name: 3, value: “101010101010″},

{name: 123, value: “hello there”},

{name: 976, value: “kjjkbcjkcbbd”},

{name: 832416, value: “kjjkbcjkcbbd”}

采用UTF8Type排序类型,结果是:

{name: 123, value: “hello there”},

{name: 3, value: “101010101010″},

{name: 832416, value: “kjjkbcjkcbbd”},

{name: 976, value: “kjjkbcjkcbbd”}

3. 实验与测试

Cassandra CLI(Command Line Interface)允许用户通过command的形式管理数据库(类似于SQL数据库的CLI),包括元数据管理,数据添加删除等,但Cassandra CLI没有像数据库那样专门的SQL语言,它只是自定义了一些类似SQL的命令,功能比SQL语言弱得多。

本节介绍了使用Cassandra CLI创建,删除keyspace,SCF,插入和删除数据的基本方法。注意,本节采用的Cassandra版本为:apache-cassandra-0.7.5。

具体流程:

首先,启动cassandra:

进入安装目录,使用命令:bin/cassandra

然后,连接到一个节点上

1
2
3
4
5
6
7
$ ./cassandra-cli -host localhost -port 9160
 
Connected to: "Test Cluster" on localhost/9160
 
Welcome to cassandra CLI.
 
Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.

之后创建一个key space和CF

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[ default @unknown] create keyspace twissandra with replication_factor=1
 
and placement_strategy= 'org.apache.cassandra.locator.SimpleStrategy' ;
 
[ default @unknown] use twissandra;
 
Authenticated to keyspace: twissandra
 
------------
 
... schemas agree across the cluster
 
[ default @twissandra]  create column family users with comparator = UTF8Type;
 
c21f48d5-8748-11e0-8afd-e700f669bcfc
 
Waiting for schema agreement...
 
... schemas agree across the cluster
 
[ default @twissandra] set users[ 'Bob' ][ 'phone' ]= '1251892983' ;
 
Value inserted.
 
[ default @twissandra] set users[ 'Bob' ][ 'address' ]= 'Haidian,Beijing' ;
 
Value inserted.
 
[ default @twissandra] set users[ 'Bob' ][ 'birthday' ]= '1980-08-09' ;
 
Value inserted.
 
[ default @twissandra] get users[ 'Bob' ];
 
=> (column=address, value=4861696469616e2c4265696a696e67, timestamp=1306380804182000)
 
=> (column=birthday, value=313938302d30382d3039, timestamp=1306380831152000)
 
=> (column=phone, value=31323531383932393833, timestamp=1306380777399000)
 
Returned 3 results.

说明:

(1) column family插入数据的方法

(2) Super column family插入数据的方法

从上图可以看出,SCF能够支持5维数据空间(分别为:keyspace,column family,super key, key, column name)。

更新phone column的值

1
2
3
[ default @twissandra]  set users[ 'Bob' ][ 'phone' ]= '1251892999' ;
 
Value inserted.

删除phone column

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[ default @twissandra] del  users[ 'Bob' ][ 'phone' ];
 
column removed.
 
[ default @twissandra] list users;
 
Using default limit of 100
 
-------------------
 
RowKey: Bob
 
=> (column=adress, value=4861696469616e2c4265696a696e67, timestamp=1306380804182000)
 
=> (column=birthday, value=313938302d30382d3039, timestamp=1306380831152000)

删除users CF

1
2
3
4
5
6
7
8
9
[ default @twissandra] drop column family users;
 
f1d9d7a6-874a-11e0-8afd-e700f669bcfc
 
Waiting for schema agreement...
 
... schemas agree across the cluster
 
------

创建SCF

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
26
27
28
29
30
31
32
33
[ default @twissandra] create column family friends with column_type=Super;
 
73264792-8740-11e0-8afd-e700f669bcfc
 
Waiting for schema agreement...
 
... schemas agree across the cluster
 
[ default @twissandra] set friends[ 'Bob' ][ 'address' ][ 'family' ]= 'BeiJing' ;
 
Value inserted.
 
[ default @twissandra] set friends[ 'Bob' ][ 'address' ][ 'company' ]= 'BeiJing' ;
 
Value inserted.
 
[ default @twissandra] get friends[ 'Bob' ][ 'address' ];
 
=> (column=636f6d70616e79, value=4265694a696e67, timestamp=1306380530572000)
 
=> (column=66616d696c79, value=4265694a696e67, timestamp=1306380522162000)
 
Returned 2 results.
 
[ default @twissandra] get friends[ 'Bob' ];
 
=> (super_column=61646472657373,
 
(column=636f6d70616e79, value=4265694a696e67, timestamp=1306380530572000)
 
(column=66616d696c79, value=4265694a696e67, timestamp=1306380522162000))
 
Returned 1 results。

在生产环境中,一般不会采用会采用Cassandra CLI进行数据和元素数据管理,而是编写thrift client。

4. 参考资料

(1) Cassandra 0.7 CLI指令手冊 – Column Family的管理(1):http://blog.labin.cc/?p=245

(2) Using the Cassandra CLI:

http://www.datastax.com/docs/0.8/cli/using_cli#creating-a-column-family

(3) WTF is a SuperColumn? An Intro to the Cassandra Data Model:

http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model

 

原创文章,转载请注明: 转载自董的博客

本文链接地址: http://dongxicheng.org/nosql/cassandra-data-model/

作者:Dong,作者介绍:http://dongxicheng.org/about/

本博客的文章集合:




http://dongxicheng.org/nosql/cassandra-data-model/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值