julykobe 2019-06-28
时间序列数据库,主键永远是时间time
一类数据
数据序列
请参考官网。本人使用docker来运行,并从官网下载客户端命令行工具influx
//下载influx客户端,其实这个压缩包已经包含了influxdb,不过我只需要客户端influx wget https://dl.influxdata.com/influxdb/releases/influxdb-1.4.2_linux_amd64.tar.gz tar xvfz influxdb-1.4.2_linux_amd64.tar.gz //将/usr/local/bin/influx 连接到influxdb-1.4.2/usr/bin/influx ln -s influxdb-1.4.2/usr/bin/influx /usr/local/bin/influx //docker docker pull registry.docker-cn.com/tutum/influxdb docker tag registry.docker-cn.com/tutum/influxdb:latest tutum/influxdb //8083是influxdb的web管理工具端口,8086是influxdb的HTTP API端口 docker run -d -p 8083:8083 -p8086:8086 --expose 8090 --expose 8099 --name influxsrv tutum/influxdb //连接客户端,默认连接到localhost:8086 influx //指定host port user pasword连接 influx其他参数请使用:influx --help //退出 exit
# 显示用户列表 docker run -d -p 8083:8083 -p8086:8086 --expose 8090 --expose 8099 --name influxsrv tutum/influxdbshow users; # 创建用户 create user "use_name" with password 'user_password'' with all privileges; # 删除用户 drop user "user_name" # 使用账号密码登陆(influx 默认不启动认证机制,需要修改配置文件来启动认证机制) influx -host 'influxdb_host' -port '8086' -username 'user_name'
show databases create database test use db_name drop database test
measurement无需创建语法,只需在插入时指定measurement即可,influx会根据你的插入的数据创建相应的measurement结构
//语法 select|insert <measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp] insert your_measurement,tag_name=tag_value... column_name=column_value //例子 insert payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i // i是integer的意思,默认数字是float //指定时间 insert payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i 1516167115623067775
select * from payment select billed from payment //查询time billed字段 //如果字段名或者measurement是关键字,那么可以使用引号。如user是关键字 //注意,不能只select *或者select field字段 select * from "user" //select device from paymet是错误的 //限制查询数量:limit //注意select的内容要么是*,要么是field select * from payment limit 10 //select "device" from payment 是错误的,因为没有field字段 //where select * from payment where billed > 10 select * from payment where "device"='mobile' #注意引号的使用 //and or not select * from payment where billed > 10 and "device"='mobile' select * from payment where time > '2018-07-19 03:00:00' select * from payment where time > now() - 1h
delete from where tag_key=tag_value and ... or ...
influxdb 是没有update的(一般数据仓库只用户数据存储与查询)
group by
select sum(billed) from payment group by "device" select sum(billed) from payment group by time(30m) //30m聚合一次数据`
常用的聚合函数有count、disinct、mean(平均值)、median(中位数)、spread(最小值和最大值之间的差值)、sum等。
select top("billed",3) from payment
常用的选择类函数有
删除整个measurement
drop measurement measurement_name;
SELECT * FROM "\"weather\""
可见https://www.linuxdaxue.com/in...
create continuous query cq_name on db_name [resample [every <interval>][for <interval>]] begin select <function><field>,... into measurement_1 from measurement_2 where... group by time(time<interval>) end //例子 create continuous query billed_30 on payment begin select sum(billed) into billed_30min from payment group by time(30m) end //这个连续查询会每30分钟执行一次
show continuous queries
drop continuous query cq_name on db_name
InfluxDB本身不提供数据的删除操作,因此用来控制数据量的方式就是定义数据保留策略。
show retention policies on db_name --------------------------output--------------------------- name duration shardGroupDuration replicaN default default 0 168h0m0s 1 true
create retention policy "policy_name" on "db_name" duration <time> replication n [default] //例子 create retention policy "3_hour" on "test" duration 3h default
alter retention policy "3_hour" on "test" duration 4h default
drop retention policy "3_hour" on "test"
doc https://docs.influxdata.com/e...
先从之前下载的压缩包中获取备份工具
ln -s influxdb-1.4.2/usr/bin/influxd /usr/local/bin/influxd
//语法 influxd backup -database db_name [since 2018-07-02 12:00:00] path_to_back //since可以指定备份某个时间点之后的数据 //例子 influxd backup payment ~/tmp/backup/payment
influxd backup -database db_name -hosthost:port [since 2018-07-05 13:00:00]path_backup
influxd restore [ -metadir | -datadir ] <path-to-meta-or-data-directory> <path-to-backup> //例子 influxd restore -database payment -metadir /var/lib/influxdb/meta -datadir /var/lib/influxdb/data ~/tmp/backup/payment //注意,在restore之前需要把influxd stop,restore后重启influxd数据才会成功恢复