1 简介
InfluxDB 是一个开源分布式时序、事件和指标数据库。使用 Go 语言编写,无需外部依赖。其设计目标是实现分布式和水平伸缩扩展。
它有三大特性:
- Time Series (时间序列):你可以使用与时间有关的相关函数(如最大,最小,求和等)
- Metrics(度量):你可以实时对大量数据进行计算
- Eevents(事件):它支持任意的事件数据
特点
- schemaless(无结构),可以是任意数量的列
- Scalable
- min, max, sum, count, mean, median 一系列函数,方便统计
- Native HTTP API, 内置http支持,使用http读写
- Powerful Query Language 类似sql
- Built-in Explorer 自带管理工具
2 CLI(Command Line Interface)操作
2.1 安装 启动
Install: brew install influxdb
Start: influxd run
Connect: influx
2.2 CLI
- 帮助信息
|
|
- 创建数据库
create database rhinotech
- 显示所有DB
show databases
|
|
使用数据库
use rhinotech
写数据
insert students,name=NikoBelic,age=18,region=china value=888,shit=999
Now that we have a database, InfluxDB is ready to accept queries and writes. First, a short primer on the datastore. Data in InfluxDB is organized by “time series”, which contain a measured value, like “cpu_load” or “temperature”. Time series have zero to many points, one for each discrete sample of the metric. Points consist of time (a timestamp), a measurement (“cpu_load”, for example), at least one key-value field (the measured value itself, e.g. “value=0.64”, or “temperature=21.2”), and zero to many key-value tags containing any metadata about the value (e.g. “host=server01”, “region=EMEA”, “dc=Frankfurt”). Conceptually you can think of a measurement as an SQL table, where the primary index is always time. tags and fields are effectively columns in the table. tags are indexed, and fields are not. The difference is that, with InfluxDB, you can have millions of measurements, you don’t have to define schemas up-front, and null values aren’t stored.
时序库与关系库相关名词对应:
时序库 | 关系库 | 备注 |
---|---|---|
time | id | 主键 |
measurement | table | 表 |
tags | column + index | 带索引的列 |
fields | column | 普通列 |
tags和fields差不多,tags自带索引,因此查询时候更快。
- 查询数
select * from students
|
|
3 HTTP 读写数据
3.1 Writing Data With Http API
- 创建数据库
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"
- 写数据库
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_info,host=server1,region=A3-1-1 load=0.64'
- 从文件导入数据
文件格式
|
|
导入 curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt
注意 如果文件中的数据数量超过5000个,建议切分成多个文件批量导入,因为http请求的默认超时时间为5s,虽然请求超时后influxdb仍然会继续导入数据,但是客户端将无法收到导入成功的提示信息。
3.2 Querying Data With Http API
- 查询
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT * FROM \"cpu_load_info\""
结果
|
|
使用 --data-urlencode "epoch=h"
可以控制显示的时间戳格式[h,m,s,ms,u,ns]