Cassandra使用 —— 一个气象站的例子
使用场景:
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','2019-08-03 07:01:00','72F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-03 07:02:00','73F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-03 07:03:00','73F');
INSERT INTO temperature(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-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 > '2019-08-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','2019-08-03','2019-08-03 07:01:00','72F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2019-08-03','2019-08-03 07:02:00','73F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2019-08-04','2019-08-04 07:01:00','73F');
INSERT INTO temperature_by_day(weatherstation_id,date,event_time,temperature) VALUES ('1234ABCD','2019-08-04','2019-08-04 07:02:00','74F');
(3)查询某个设备某一天的数据
SELECT * FROM temperature_by_day WHERE weatherstation_id='1234ABCD' AND date='2019-08-03';

三、方案3:存储带时效性的数据,过期就自动删除
对于时序的数据的另外一种典型应用就是要做循环存储,想象一下,比如我们要在一个dashboard展示最新的10条温度数据,老的数据就没用了,可以不用理会。如果使用其他的数据库,我们往往需要设置一个后台的job去对历史数据做定时清理。但是使用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','2019-08-03 07:03:00','72F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-03 07:02:00','73F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-03 07:01:00','73F') USING TTL 20;
INSERT INTO latest_temperatures(weatherstation_id,event_time,temperature) VALUES ('1234ABCD','2019-08-03 07:04:00','74F') USING TTL 20;
(3)观察
在插入数据之后,你可以不断的使用查询语句来看这些数据,我们可以看到他们一条一条的消失,直到最后所有都没了。

time-series,其是Cassandra最有竞争力的数据模型之一
原文摘要:
Cassandra can store up to 2 billion columns per row
参考资料:
https://academy.datastax.com/resources/getting-started-time-series-data-modeling
http://www.rubyscale.com/post/143067470585/basic-time-series-with-cassandra
http://www.datastax.com/dev/blog/advanced-time-series-with-cassandra
Cassandra使用 —— 一个气象站的例子的更多相关文章
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- Spring-Context之一:一个简单的例子
很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...
- 高仿“点触验证码”做的一个静态Html例子
先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...
- 关于apriori算法的一个简单的例子
apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...
- 一个UWSGI的例子
摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...
- 扩展Python模块系列(二)----一个简单的例子
本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...
- fitnesse - 一个简单的例子(slim)
fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行 2.1 新建wikiPage 2.2 运行 ...
- Struts2的配置和一个简单的例子
Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...
- 一个简单的例子搞懂ES6之Promise
ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...
随机推荐
- Ambiguous mapping. Cannot map 'xxxController' method
@GetMapping public JsonResp<List<DtoLandRegion>> getLandRegionList() { List<DtoLandRe ...
- Java 循环语句及流程控制语句
java循环语句while与do-while 一 while循环 while循环语句和选择结构if语句有些相似,都是根据条件判断来决定是否执行大括号内的执行语句. 区别在于,while语句会反复地进行 ...
- 定宽整形(C++11起)
定义于头文件 cstdint中int8_t.int16_t.int32_t.int64_t 分别为宽度恰为 8 . 16 . 32 和 64 位的有符号整数类型无填充位并对负值使用补码(仅若实现支持该 ...
- 数据结构C++实现邻接矩阵存储图
定义邻接矩阵存储的图类.[实验要求] 1. 创建一个邻接矩阵存储的图: 2. 返回图中指定边的权值: 3. 查找图中某顶点的第一个邻接顶点.某顶点关于另一个顶点的下一个邻接顶点序号: 4. 图的深度优 ...
- Vue Slots
子组件vue <template> <div> <slot v-if="slots.header" name="header"&g ...
- Centos7查看端口占用
(1)netstat -lnp|grep 50090 如果提示没有netstat命令,可需要安装:yum -y install net-tools (2) lsof -i:50090 参考链接:lin ...
- CentOS 安装、配置Nginx反向代理
安装: yum install epel-release yum install nginx 配置: [root@bogon ~]# vim /etc/nginx/conf.d/default.con ...
- Discovering Reinforcement Learning Algorithms
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:2007.08794v1 [cs.LG] 17 Jul 2020 Abstract 强化学习(RL)算法根据经过多年研究手动发 ...
- NumPy笔记-ndarray
ndarray,N维数组对象(矩阵) 所有元素必须是相同类型 ndim属性,维度个数 shape属性,各维度大小 dtype属性,数据类型 创建ndarray np.array(collection) ...
- win7中java编程工具安装 java环境变量设置
一.下载java 官方地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-download-400750.html ...