日期:2014-05-16  浏览次数:20476 次

走进cassandra之二 数据模型

关于数据模型的介绍, 这里有一篇文章,讲的挺好的。
http://blog.csdn.net/wh62592855/article/details/5721152
这里我就不再赘述,稍微讲点不一样的。


在1.1官方的文档里面,列族被分成了两类:
static column family
dynamic column famliy


static这种,就是传统上的,大家很快就能理解的类似于关系型数据库的table的一种,官方定义如下:
A static column family uses a relatively static set of column names and is similar to a relational database table. For
example, a column family storing user data might have columns for the user name, address, email, phone number and
so on. Although the rows generally have the same set of columns, they are not required to have all of the columns
defined. Static column families typically have column metadata pre-defined for each column.
这段英文不难看懂,也很容易理解,跟table有很大的相似性。
 
关于dynamic这种,就是新的概念了,官方定义:
A dynamic column family takes advantage of Cassandra's ability to use arbitrary application-supplied column names to
store data. A dynamic column family allows you to pre-compute result sets and store them in a single row for efficient
data retrieval. Each row is a snapshot of data meant to satisfy a given query, sort of like a materialized view. For
example, a column family that tracks the users that subscribe to a particular user's blog is dynamic
 
这段文字有点费解,尤其是刚刚学习NOSQL的时候。
但是他举的例子,可以比较快接受,大家现在都用微博,如果追踪一个成名人物所有的粉丝,为这个目的,建立一个列族的话,就可用 dynamic这种。
(你用关系型数据库的表,也能存储这种关系,但是记得,我们现在是在nosql的世界里哦)
总的来说,各个column不是固定的,是动态的,这就是dynamic.
 
关于每个column,1.1官方文档里也做了描述,它区分的更加细致,除了大家所了解的普通列和超级列,还有其他的,如下:
 
Column families consist of these kinds of columns:
? Standard: Has one primary key.
? Composite: Has more than one primary key, recommended for managing wide rows
? Expiring: Gets deleted during compaction.
? Counter: Counts occurrences of an event.
? Super: Used to manage wide rows, inferior to using composite columns.
 

 
关于cassandra有很多文档上都说它是无schema,其实这个描述,稍微有点不准确,schema是什么呢,就是一个数据库的一组规则标准,不管你是关系型,还是nosql,总得有规则的,不可能是一点儿没有,区别在于 cassandra的这种, schema有点粗,相当大条。
 
怎么个粗法呢?
 
比如说我们找到一个schema文件的例子
xxxx\services\schema
 
我们可以打开一个cv.cass来看看。如下:
CREATE KEYSPACE CV;
 
USE CV;
 
CREATE COLUMN FAMILY Comments
WITH column_type = Super
AND comparator = TimeUUIDType
AND key_validation_class = BytesType;
 
CREATE COLUMN FAMILY CommentCounter
WITH default_validation_class = CounterColumnType
AND key_validation_class = BytesType
AND comparator = UTF8Type;
 
CREATE COLUMN FAMILY Group
WITH comparator = UTF8Type
AND key_validation_class = BytesType
AND column_metadata=[
{column_name: name, validation_class: UTF8Type, index_type: KEYS},
{column_name: type, validation_class: UTF8Type, index_type: KEYS},
{column_name: timecreated, validation_class: IntegerType, index_type: KEYS},
{column_name: timeupdated, validation_class: IntegerType, index_type: KEYS}];
 
可以看出,这里面,就列出了 列族的名字, 还有一些基本属性,比起关系型数据库的 E-R图来说,是有些大条,以至于说它是无 schema了。
 
那么这段 粗略的schema,到底 规定了什么东东呢?到底在说呢?
 
CREATE COLUMN FAMILY Comments 给列族起了一个名字,这个好理解。
WITH column_type = Super 这段是说这个列族是超级列族。
AND comparator = TimeUUIDType 这个是规定列名的数据类型和列是如何排序的。官方的解释是The comparator specifies the data type
for the column name, as well as the sort order in which columns are stored within a row。
AND key_validation_class = BytesType; 这个是定义row key validator的
 
WITH default_validation_class = CounterColumnType 这个也是validator。
 
AND column_metadata=[
{column_name: name, validation_class: UTF8Type, index_type: KEYS},
{column_name: type, validation_class: UTF8Type, index_type: KEYS},
{column_name: timecreated, validation_class: IntegerType, index_type: KEYS},
{column_name: timeupdated, validation_class: IntegerType, index_type: KEYS}];
这一堆,说的是针对 静态列族,你可以预先定义一部分列。这些列是预