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

参考资料:


附件列表

如何使用Cassandra来存储time-series类型的数据的更多相关文章

  1. Cassandra存储time series类型数据时的内部数据结构?

        因为我一直想用Cassandra来存储我们的数字电表中的数据,按照之前的文章(getting-started-time-series-data-modeling)的介绍,Cassandra真的 ...

  2. c#学习基础(2)存储、值类型和引用类型、变量

    程序运行时,它的数据必须存储在内存中,数据项需要多大的内存.存储在什么地方以及如何存储都依赖该数据项的类型 运行中的程序使用两个区域来存储数据:栈和堆 栈是一个内存数组,是一个LIFO(last in ...

  3. JanusGraph :Cassandra作为存储后端的情况下,JanusGraph的安装方法

    Cassandra作为存储后端的情况下,JanusGraph的安装方法 Cassandra作为存储后端的情况下,JanusGraph的安装分为四种方式. 分别是: 1.本地服务器模式(这里的服务器指的 ...

  4. pandas库Series类型与基本操作

    pandas读取excel的类型是dataFrame,然后提取每一列是一个Series类型 Series类型包括index和values两部分 a = pd.Series({'a':1,'b':5}) ...

  5. 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  6. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  7. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作

    http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...

  8. C# Winform中执行post操作并获取返回的XML类型的数据

    /// <summary> /// 返回指定日期的订单数据 /// </summary> /// <param name="StartDate"> ...

  9. ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理

    分布式文档存储 ES分布式特性 屏蔽了分布式系统的复杂性 集群内的原理 垂直扩容和水平扩容 真正的扩容能力是来自于水平扩容–为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中 ES集群特点 ...

随机推荐

  1. Java中BigDecimal类介绍及用法

    Java中提供了大数字(超过16位有效位)的操作类,即 java.math.BinInteger 类和 java.math.BigDecimal 类,用于高精度计算. 其中 BigInteger 类是 ...

  2. django----用户认证(auth模块)

    用法 from django.contrib import auth user = authenticate(username='someone',password='somepassword') l ...

  3. hdu3397区间覆盖,区间翻转,区间合并,区间求和

    调了很久的代码..注意区间翻转和覆盖的操作互相的影响 /* 区间替换操作怎么搞? 应该是加个tag标记 如果整个区间都是0|1,那么把若有tag的话直接set1|0即可,也不用设置tag标记 反之要设 ...

  4. pytest三:fixture_conftest.py 自定义测试用例的预置条件(setup)

    用例加 setup 和 teardown 可以实现在测试用例之前或之后加入一些操作,但返种是整个脚本全局生效的,如果我想实现以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录. ...

  5. python 全栈开发,Day125(HTML5+ 初识,HBuilder,夜神模拟器,Webview)

    昨日内容回顾 1.增删改查: 增: db.collections.insert({a:1}) // 官方不推荐了 db.collections.insertMany([{a:1},{b:1}]) in ...

  6. 【【C++ Primer 第15章】 虚析构函数

    学习资料 • C++中基类的析构函数为什么要用virtual虚析构函数 虚析构函数 1. 正文 直接的讲,C++中基类采用virtual虚析构函数是为了防止内存泄漏.具体地说,如果派生类中申请了内存空 ...

  7. Code alignment 代码对齐改进(VS2017)

    In mathematics you always keep your equals lined up directly underneath the one above. It keeps it c ...

  8. Springboot实现热部署

    所谓的热部署:比如项目的热部署,就是在应用程序在不停止的情况下,实现新的部署 而Springboot在我们每次修改完代码之后,可能只是修改下打印的信息,就得重新启动App类,这样太浪费时间,有没有一种 ...

  9. python单例模式的实现

    1 线程不安全的单例模式 # -*- coding:utf-8 -*- from __future__ import unicode_literals import functools def sin ...

  10. Storm消息容错机制(ack-fail机制)

    storm消息容错机制(ack-fail) 1.介绍 在storm中,可靠的信息处理机制是从spout开始的. 一个提供了可靠的处理机制的spout需要记录他发射出去的tuple,当下游bolt处理t ...