走进cassandra之二 数据模型

在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.
 sql

 
关于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}];
这一堆,说的是针对 静态列族,你能够预先定义一部分列。这些列是预先定义好的,死的,不是在运行时才加上去的。
比较适合有公共部分的row,好比说,一个‘人‘的列族,每人确定是有性别的,别的能够没有,这个必须有。
 
通常来讲,做为咱们程序员,只关心列族的配置就行了,keyspace的,能够不太关注。
 
数据模型,是了解cassandra的hello world 步骤,过了这一篇,下面就是高年级知识了。数据库

相关文章
相关标签/搜索