2014-02-15
Tags: cassandra
使用 Cassandra 時要設計出合適的 Data Model 是件苦差事,這篇是基於 Cassandra 2.0 版本整理出的一些心得,有興趣的人可以參考一下。以下...正文開始!
目前找到比較好而且完整的文章是「Cassandra Data Modeling Best Practices」這份文章(
簡中版 part1,
簡中版 part2)(
原文版 part1,
原文版 part2),建議先把它整個讀過一遍。讀完後接著可以看看原作著以這份文章為基礎整理出來的下面這份投影片
PRIMARY KEY(partition_key,clustering_column_1,clustering_column_2)partition_key 對應到 column family 的 row key,clustering_column_1、clustering_column_2 則是 composite column
PRIMARY KEY((partition_key1,partition_key2),clustering_column_1,clustering_column_2,clustering_column_3)partition_key1 + partition_key2 組合了 column family 的 row key,clustering_column_1、clustering_column_2、clustering_column_3 則是 composite column
CREATE TABLE passbook (
user varchar,
date timestamp,
deposit int,
withdraw int,
PRIMARY KEY(user,date)
);
INSERT INTO passbook (user, date, deposit,withdraw) VALUES ('nekobean','2014-01-01',1000,10);
INSERT INTO passbook (user, date, withdraw) VALUES ('nekobean','2014-01-02',20);
INSERT INTO passbook (user, date, withdraw) VALUES ('nekobean','2014-01-03',30);
INSERT INTO passbook (user, date, withdraw) VALUES ('duke','2014-02-01',300);
INSERT INTO passbook (user, date, withdraw) VALUES ('duke','2014-02-02',200);
INSERT INTO passbook (user, date, deposit, withdraw) VALUES ('duke','2014-02-03',2000, 100);