Cassandra非常適合存儲時序類型的數據,本文我們將使用一個氣象站的例子,該氣象站每分鍾需要存儲一條溫度數據。
一、方案1,每個設備占用一行
這個方案的思路就是給每個數據源創建一行,比如這里一個氣象站的溫度就占用一行,然后每個分鍾要采集一個溫度,那么就讓每個時刻的時標將作為列名,而溫度值就是列值。
(1) 創建表的語句如下:
CREATE TABLE temperature (
weatherstation_id text,
event_time timestamp,
temperature text,
PRIMARY KEY (weatherstation_id,event_time) );
(2)然后插入如下數據。
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:01:00','72F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:02:00','73F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:03:00','73F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:04:00','74F');
(3) 如果要查詢這個氣象站的所有數據,則如下
SELECT event_time,temperature FROM temperature WHERE weatherstation_id='1234ABCD';
(4) 如果要查詢某個時間范圍的數據,則如下:
SELECT temperature FROM temperature WHERE weatherstation_id='1234ABCD' AND event_time > '2013-04-03 07:01:00'

二、方案2,每個設備的每天的數據占用一行
有時候把一個設備的所有數據存儲在一行可能有點困難,比如放不下(這種情況應該很少見),此時我們就可以對上一個方案做拆分,在row key中增加一個表示,比如可以限制把每個設備每一天的數據放在單獨一行,這樣一行的數量大小就可控了。
(1) 創建表
CREATE TABLE temperature_by_day (
weatherstation_id text,
date text,
event_time timestamp,
temperature text,
PRIMARY KEY ((weatherstation_id,date),event_time) );
(2)插入數據
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2013-04-03','2013-04-03 07:01:00','72F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2013-04-03','2013-04-03 07:02:00','73F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2013-04-04','2013-04-04 07:01:00','73F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2013-04-04','2013-04-04 07:02:00','74F');
(3)查詢某個設備某一天的數據
SELECT * FROM temperature_by_day WHERE weatherstation_id='1234ABCD' AND date='2013-04-03';

三、方案3,存儲帶時效性的數據,過期就自動刪除
對於時序的數據的另外一種典型應用就是要做循環存儲,想象一下,比如我們要在一個dashboard展示最新的10條溫度數據,老的數據就沒用了,可以不用理會。如果使用其他的數據庫,我們往往需要設置一個后台的job去對歷史數據做定時清理,我們現在使用pg的時候就是這么干的。但是使用Cassandra,我們可以使用Cassandra的一個叫做
過期列(expiring colmn)的新特性,只要超過指定的時間,這個列就自動消失了。
(1) 創建表
CREATE TABLE latest_temperatures (
weatherstation_id text,
event_time timestamp,
temperature text,
PRIMARY KEY (weatherstation_id,event_time),
)
WITH CLUSTERING ORDER BY (event_time DESC);
(2)插入數據
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:03:00','72F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:02:00','73F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:01:00','73F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2013-04-03 07:04:00','74F') USING TTL 20;
(3)觀察
在插入數據之后,你可以不斷的使用查詢語句來看這些數據,我們可以看到他們一條一條的消失,直到最后所有都沒了。

總結:
time-series是Cassandra最有競爭力的數據模型之一,
原文摘要:
1) Cassandra can store up to 2 billion columns per row
參考資料:
附件列表