我以前的节点数据是全都存到mongoDB中的。偶然的机会,发现了InfluxDB这种时序数据库。因而花了点时间简单使用一下,看看是否能替换mongoDB存储历史数据。javascript
InfluxDB是一个开源时序数据库。适合处理分析资源数据监控相关数据。java
docs.influxdata.com/influxdb/v1…mysql
目前最新版本为v1.6。git
安装InfluxDB的方式很简单(Ubuntu)github
curl -sL <https://repos.influxdata.com/influxdb.key> | sudo apt-key add -
source /etc/lsb-release
echo "deb <https://repos.influxdata.com/${DISTRIB_ID,,}> ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt-get update && sudo apt-get install influxdb
sudo service influxdb start
复制代码
完成。😋web
而后就能够在命令行里输入influx愉快的玩耍了。sql
使用命令行建立一些数据shell
create database 'mydb'
use mydb
insert test,tag1=111,tag2=222 field1=111111,field2=222222
复制代码
而后就能够查询数据。因为是新手,固然是使用工具啦。数据库
之前的版本自带一个web管理界面。如今的版本没有了~npm
附上可视化工具一枚github.com/CymaticLabs…
插入成功😄能够看到里面除了咱们本身加的两个tag,两个field,还包括一个自动添加的time。毕竟是时序数据库
嘛!
除了使用InfluxDBStudio之外,还可使用另外一个工具 Grafana。grafana.com 感兴趣的能够本身去查查。嗯,很炫。
npm 包 www.npmjs.com/package/inf…
npm install --save influx@next
复制代码
const Influx = require('influx'); //导包
// 定义数据库链接和数据格式,建立client
const client = new Influx.InfluxDB({
database: 'mydb',
username: 'root',
password: 'root',
hosts: [{ host: 'xx.xx.xx.xxx' }],
schema: [
{
measurement: 'test', //相似于数据表的概念
fields: { //数据表的字段,定义类型,FLOAT/INTEGER/STRING/BOOLEAN
field1:Influx.FieldType.INTEGER,
field2:Influx.FieldType.INTEGER,
}, // tag 也是里面的字段,是自带索引光环。查询速度杠杠的。
tags: ['tag1','tag2']
}
]
});
// 插入数据
client.writePoints([
{
measurement: 'test',
fields: {
field1:1231123,
field2:44233,
},
tags: {
tag1:14233,
tag2:41122
}
}
])
复制代码
const Influx = require('influx');
// 和上边同样。
const client = new Influx.InfluxDB({
database: 'mydb',
username: 'root',
password: 'root',
hosts: [{ host: 'xx.xx.xx.xxx' }],
schema: [
{
measurement: 'test', //相似于数据表的概念
fields: { //数据表的字段,定义类型,FLOAT/INTEGER/STRING/BOOLEAN
field1:Influx.FieldType.INTEGER,
field2:Influx.FieldType.INTEGER,
}, // tag 也是里面的字段,是自带索引光环。查询速度杠杠的。
tags: ['tag1','tag2']
}
]
});
// 获取5分钟内的数据
// 至于Influx的查询语法,听说和mysql有点相似。尚未具体研究。如今就简单查一个吧
client
.query(
` SELECT * FROM "test" WHERE time > now() - 5m `
)
.then(res => {
console.log(res); //输出一个数组。
});
复制代码
到目前为止,已经基本知足项目需求。接下来就是各类花式查询而已。