风泥

导航

时序数据库InfluxDB

时序数据库InfluxDB


什么是时间序列数据库?最简单的定义就是数据格式里包含Timestamp字段的数据,比如某一时间环境的温度,CPU的使用率等。但是,有什么数据不包含Timestamp呢?几乎所有的数据其实都可以打上一个Timestamp字段。时间序列数据的更重要的一个属性是如何去查询它,包括数据的过滤,计算等等

各时序数据库流行排名:https://db-engines.com/en/ranking/time+series+dbms


influxDB

  1. 简介
    官网:https://www.influxdata.com/
    参考文档:https://docs.influxdata.com/influxdb/v1.3/administration/config/#configuration-options-overview
    参考文档:https://yq.aliyun.com/articles/104243

    • InfluxDB是一个开源的分布式时序、时间和指标数据库,使用go语言编写,无需外部依赖
    • InfluxDB支持的时间级别到了纳秒(ns),所以到数据的操作也应该到纳秒级。
  2. 安装

    • 解压后会得到下文件

      influxd          // influxdb服务器
      influx           // influxdb命令行客户端
      influx_inspect   // 查看工具
      influx_stress    // 压力测试工具
      influx_tsm       // 数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)
    • Widnows以管理员身份在powershell中运行influx, 直接在CMD中运行无法进入influx脚本命令

  3. 访问方式
    参考文档:http://blog.csdn.net/u010185262/article/details/53158786

    • 命令行:

      //创建数据库
      create database "db_name"
      //显示所有的数据库
      show databases
      //删除数据库
      drop database "db_name"
      //使用数据库
      use db_name
      //显示该数据库中所有的表
      show measurements
      //创建表,直接在插入数据的时候指定表名
      insert test,host=127.0.0.1,monitor_name=test count=1
      //删除表
      drop measurement "measurement_name"
      
      use testDb
      insert test,host=127.0.0.1,monitor_name=test count=1
      //注意:使用命令行操作数据库,除了必要的空隔符,不要随意添加空隔,下面语句将报错,提示:missing tag key
      // insert test,host=127.0.0.1,monitor_name=test count=1
      
      eg:
      select count(*) from CoordsOriginal where tagID='DECA01001000158F' and time > '2017-10-09 00:00:00'
      
    • http接口:

      • GET: SELECT* SHOW
      • POST: ALTER CREATE DELETE DROP GRANT KILL REVOKE
      • 数据写入:

        curl -i -XPOST 'http://127.0.0.1:8086/write?db=testDb' --data-binary 'test,host=127.0.0.1,monitor_name=test count=1'
    • 关键字

      • epoch[ns,u,µ,ms,s,m,h]
        设置时间显示格式,默认为UTC(2017-12-21T14:09:07.435Z), 添加epoch将显示为时间戳
    • 数据类型
      参考文档:https://docs.influxdata.com/influxdb/v1.6/write_protocols/line_protocol_tutorial/#data-types

      Datatype Element(s) Description
      Float Field values IEEE-754 64-bit floating-point numbers. This is the default numerical type. Examples: 1, 1.0, 1.e+78, 1.E+78.
      Integer Field values Signed 64-bit integers (-9223372036854775808 to 9223372036854775807). Specify an integer with a trailing i on the number. Example: 1i.
      String Measurements, tag keys, tag values, field keys, field values Length limit 64KB.
      Boolean Field values Stores TRUE or FALSE values.TRUE write syntax:[t, T, true, True, TRUE].FALSE write syntax:[f, F, false, False, FALSE]
      Timestamp Timestamps Unix nanosecond timestamp. Specify alternative precisions with the HTTP API. The minimum valid timestamp is -9223372036854775806 or 1677-09-21T00:12:43.145224194Z. The maximum valid timestamp is 9223372036854775806 or 2262-04-11T23:47:16.854775806Z.
    • 使用

      • 创建数据库
      • 创建数据表
      • 增添数据

        +-----------+--------+-+---------+-+---------+
        |measurement|,tag_set| |field_set| |timestamp|
        +-----------+--------+-+---------+-+---------+
        measurement :等同于table
        tag_set     :列名,所有的tag都会被索引
        field_set   :列名,不会被索引
        timestamp   :时间戳,如果不传值,influxdb在写入该point的时候,会自动生成
        
        //上面四个字段每一组都用“ ”分开
        eg: insert Table,tag1=xx,tag2=xx,tag3=xx field1=xx,filed2=xx,filed3=xx timestamp
        
        //tag_set无法单独查找tag_set列,可以单独查找field_set列
        //官方原话:Note that you must specify at least one field in the `SELECT`
        eg(error): select tag_set from Table
        eg(correct): select field_set from Table
        eg(correct): select tag_set,field_set from Table    
        
      • 修改数据

      • 删除数据
      • 查找数据

        • 命令行
          
          //根据时间排序,查找10条数据
          select * from table order by time desc limit 10
          
          //now(), 时间单位[ns,u,µ,ms,s,m,h]
          select * from table where time>now()-1h
          
      • Filter(过滤)
        select * from table where cloumn0=’xx’ and cloumn1=’xx’

      • Aggregation(聚合) : SUM AVG Max TopN等
        select avg(x) from table group by cloumn0
        说明:group by 是基于相同时间粒度做聚合; pre-aggregation(预聚合,数据实时写入后就经过预聚合运算),提搞效率

      • Downsampling(降精度),Rollup(汇总)和Auto-rollup(自动汇总)
        downsampling from interval(10) to interval(30), //10秒精度的数据聚合成30秒精度的数据

    • 配置
      官方文档:https://docs.influxdata.com/influxdb/v1.3/administration/config/#configuration-options-overview
      参考文档:http://www.cnblogs.com/mafeng/p/6848166.html

    • UI(Chronograf)
      官方文档:https://docs.influxdata.com/chronograf/v1.3/
      influxDB提供Chronograf通过浏览器查看数据库(localhost:8888),需要使用Google Chrome浏览器

    • 应用示例(telegraf)
      官方文档:https://docs.influxdata.com/telegraf/v1.4/introduction/getting_started/
      InfluxDB提供Telegraf实时监控电脑性能,并可以通过Chronograf查看。
      Chronograf查看Telegraf数据

    • 数据备份 / 恢复
      官方文档:https://docs.influxdata.com/influxdb/v1.3/administration/backup_and_restore/#backing-up-a-database
      参考文档:https://www.linuxdaxue.com/influxdb-backup-and-restore.html
      • 数据备份:
        备份时需要先运行Influxd服务
        influxd backup -database <mydatabase> <path-to-backup>
        eg:
        这里写图片描述
    • 数据恢复
      meta目录与data目录都需要恢复
      eg:
      这里写图片描述
      注:需要在配置中指定恢复目录
      这里写图片描述

posted on 2017-12-22 10:53  风泥  阅读(1028)  评论(0编辑  收藏  举报