influxDB 常用操作

参考文献

influxdb文档 - 聚合函数说明

使用help查看常用命令

> help
Usage:
        connect <host:port>   connects to another node specified by host:port
        auth                  prompts for username and password
        pretty                toggles pretty print for the json format
        chunked               turns on chunked responses from server
        chunk size <size>     sets the size of the chunked responses.  Set to 0 to reset to the default chunked size
        use <db_name>         sets current database
        format <format>       specifies the format of the server responses: json, csv, or column
        precision <format>    specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns
        consistency <level>   sets write consistency level: any, one, quorum, or all
        history               displays command history
        settings              outputs the current settings for the shell
        clear                 clears settings such as database or retention policy.  run 'clear' for help
        exit/quit/ctrl+d      quits the influx shell

        show databases        show database names
        show series           show series information
        show measurements     show measurement information
        show tag keys         show tag key information
        show field keys       show field key information

        A full list of influxql commands can be found at:
        https://docs.influxdata.com/influxdb/latest/query_language/spec/
> 

查看数据库

> show databases
name: databases
name
----
_internal
nmon_reports
nmon2influxdb_log
> 

删除数据库

> drop database nmon_reports
> drop database nmon2influxdb_log

使用\切换数据库

> use nmon_reports
Using database nmon_reports

查看所有表数据

> show measurements
name: measurements
name
----
CPU_ALL
...
> 

删除单表数据

> drop measurement CPU_ALL

查看所有表中的索引tag

> show tag keys;
name: cpu
tagKey
------
cpu
host

name: disk
tagKey
------
device
fstype
host
mode
path

name: diskio
tagKey
------
host
name

查看表中有哪些字段是tag

> show tag keys from cpu;
name: cpu
tagKey
------
cpu
host
> 

查看表中有哪些field字段

> show field keys from cpu;
name: cpu
fieldKey         fieldType
--------         ---------
usage_guest      float
usage_guest_nice float
usage_idle       float
usage_iowait     float
usage_irq        float
usage_nice       float
usage_softirq    float
usage_steal      float
usage_system     float
usage_user       float
> 

查询表中的点series

> show series from cpu;
key
---
cpu,cpu=cpu-total,host=QC_PREDEPL_API_02
cpu,cpu=cpu-total,host=QC_PREDEPL_CMS-NGX-02
cpu,cpu=cpu-total,host=QC_PREDEPL_Mongo_Mysql_02
cpu,cpu=cpu-total,host=QC_PREDEPL_Redis_Memcache_02
cpu,cpu=cpu-total,host=locust01
cpu,cpu=cpu-total,host=locust02

查询单表10条数据

> select * from CPU_ALL limit 10
name: CPU_ALL
time                host                  name   value
----                ----                  ----   -----
1551694907000000000 qc_predepl_cms-ngx-02 CPUs   4
1551694907000000000 qc_predepl_cms-ngx-02 Idle%  96.9
1551694907000000000 qc_predepl_cms-ngx-02 Steal% 0
1551694907000000000 qc_predepl_cms-ngx-02 Sys%   0.9
1551694907000000000 qc_predepl_cms-ngx-02 User%  0.7
1551694907000000000 qc_predepl_cms-ngx-02 Wait%  1.5
1551694910000000000 qc_predepl_cms-ngx-02 CPUs   4
1551694910000000000 qc_predepl_cms-ngx-02 Idle%  99.7
1551694910000000000 qc_predepl_cms-ngx-02 Steal% 0
1551694910000000000 qc_predepl_cms-ngx-02 Sys%   0.1
> 

查询表中最大的值

> select max(*) from CPU_ALL
name: CPU_ALL
time                max_value
----                ---------
1551695447000000000 100
> 

设置时间显示格式

在influxDB的CLI界面执行precision rfc3339即可,但是显示是UTC的时区,与中国时区差了8个小时,需要在查询语句的最后加上tz('Asia/Shanghai'),这样查询的时间才是纠正为中国时区显示。

> precision rfc3339

> select * from CPU_ALL where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' limit 10

name: CPU_ALL
time                 host                  name   value
----                 ----                  ----   -----
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 CPUs   4
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 Idle%  96.9
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 Steal% 0
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 Sys%   0.9
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 User%  0.7
2019-03-04T10:21:47Z qc_predepl_cms-ngx-02 Wait%  1.5
2019-03-04T10:21:50Z qc_predepl_cms-ngx-02 CPUs   4
2019-03-04T10:21:50Z qc_predepl_cms-ngx-02 Idle%  99.7
2019-03-04T10:21:50Z qc_predepl_cms-ngx-02 Steal% 0
2019-03-04T10:21:50Z qc_predepl_cms-ngx-02 Sys%   0.1
> 

> select * from CPU_ALL where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' limit 10 tz('Asia/Shanghai')
name: CPU_ALL
time                      host                  name   value
----                      ----                  ----   -----
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 CPUs   4
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 Idle%  96.9
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 Steal% 0
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 Sys%   0.9
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 User%  0.7
2019-03-04T18:21:47+08:00 qc_predepl_cms-ngx-02 Wait%  1.5
2019-03-04T18:21:50+08:00 qc_predepl_cms-ngx-02 CPUs   4
2019-03-04T18:21:50+08:00 qc_predepl_cms-ngx-02 Idle%  99.7
2019-03-04T18:21:50+08:00 qc_predepl_cms-ngx-02 Steal% 0
2019-03-04T18:21:50+08:00 qc_predepl_cms-ngx-02 Sys%   0.1
> 

条件查询

  • 查询某时间下的数据
> select * from CPU_ALL where "time" = 1551694910000000000
name: CPU_ALL
time                host                  name   value
----                ----                  ----   -----
1551694910000000000 qc_predepl_cms-ngx-02 CPUs   4
1551694910000000000 qc_predepl_cms-ngx-02 Idle%  99.7
1551694910000000000 qc_predepl_cms-ngx-02 Steal% 0
1551694910000000000 qc_predepl_cms-ngx-02 Sys%   0.1
1551694910000000000 qc_predepl_cms-ngx-02 User%  0.2
1551694910000000000 qc_predepl_cms-ngx-02 Wait%  0
> 
  • 查询某个时间返回的数据,设置时区为上海时区
> select * from CPU_ALL where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')
  • 查询特定字段数据

select * from table_name where "字段1" =~ /匹配值/

> select * from CPU_All3 limit 10
name: CPU_All3
time                Cpus Idle% Steal% Sys% User% Wait% host
----                ---- ----- ------ ---- ----- ----- ----
1551689409000000000 4    94.5  0      0.9  0.7   3.9   qc_predepl_cms-ngx-02
1551689412000000000 4    99.8  0      0.2  0.1   0     qc_predepl_cms-ngx-02
1551689415000000000 4    99.5  0      0    0.1   0.4   qc_predepl_cms-ngx-02
1551689418000000000 4    99.4  0      0.1  0.1   0.4   qc_predepl_cms-ngx-02
1551689421000000000 4    99.7  0      0.2  0.2   0     qc_predepl_cms-ngx-02
1551689424000000000 4    99.7  0      0.1  0.1   0.2   qc_predepl_cms-ngx-02
1551689427000000000 4    99.5  0      0.2  0.2   0.2   qc_predepl_cms-ngx-02
1551689430000000000 4    99.7  0      0.2  0.2   0     qc_predepl_cms-ngx-02
1551689433000000000 4    99.7  0      0.1  0.2   0.1   qc_predepl_cms-ngx-02
1551689436000000000 4    99.8  0      0.1  0.1   0     qc_predepl_cms-ngx-02
> 
> 
> SELECT * FROM "CPU_All3" WHERE time < now() - 5m and "Idle%" =~ /94/
name: CPU_All3
time                Cpus Idle% Steal% Sys% User% Wait% host
----                ---- ----- ------ ---- ----- ----- ----
1551689409000000000 4    94.5  0      0.9  0.7   3.9   qc_predepl_cms-ngx-02
1551694925000000000 4    94.8  0      3.5  1.3   0.4   qc_predepl_cms-ngx-02
1551694937000000000 4    94.2  0      4.3  1.3   0.3   qc_predepl_cms-ngx-02
> 
> SELECT * FROM "CPU_All3" WHERE time < now() - 5m and "Idle%" =~ /94.5/
name: CPU_All3
time                Cpus Idle% Steal% Sys% User% Wait% host
----                ---- ----- ------ ---- ----- ----- ----
1551689409000000000 4    94.5  0      0.9  0.7   3.9   qc_predepl_cms-ngx-02
> 
> 
> SELECT * FROM "CPU_All3" WHERE time < now() - 5m and "Idle%" =~ /94.5/ and host =~ /qc_predepl_cms/
name: CPU_All3
time                Cpus Idle% Steal% Sys% User% Wait% host
----                ---- ----- ------ ---- ----- ----- ----
1551689409000000000 4    94.5  0      0.9  0.7   3.9   qc_predepl_cms-ngx-02
> 

倒序查询

> select * from CPU_ALL order by time desc limit 10 tz('Asia/Shanghai')
name: CPU_ALL
time                      host                  name   value
----                      ----                  ----   -----
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 Wait%  0.3
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 User%  0.1
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 Sys%   0.1
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 Steal% 0
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 Idle%  99.5
2019-03-04T18:31:44+08:00 qc_predepl_cms-ngx-02 CPUs   4
2019-03-04T18:31:41+08:00 qc_predepl_cms-ngx-02 Wait%  0
2019-03-04T18:31:41+08:00 qc_predepl_cms-ngx-02 User%  0.1
2019-03-04T18:31:41+08:00 qc_predepl_cms-ngx-02 Sys%   0.2
2019-03-04T18:31:41+08:00 qc_predepl_cms-ngx-02 Steal% 0

Distinct去重查询

> SELECT COUNT(DISTINCT("level description")) FROM "h2o_feet"

name: h2o_feet
time                   count
----                   -----
1970-01-01T00:00:00Z   4

Max()最大值、Min() 最小值

> select min(*) from CPU_ALL tz('Asia/Shanghai')
name: CPU_ALL
time                      min_value
----                      ---------
2019-03-04T18:21:47+08:00 0
> 
> select max(*) from CPU_ALL tz('Asia/Shanghai')
name: CPU_ALL
time                      max_value
----                      ---------
2019-03-04T18:30:47+08:00 100
> 

Mean()查询平均值

> select mean(*) from CPU_ALL tz('Asia/Shanghai')
name: CPU_ALL
time                      mean_value
----                      ----------
1970-01-01T08:00:00+08:00 17.336166666666678
> 

MEDIAN() 中位数

返回查询结果中间的数值。

> select median(*) from CPU_ALL tz('Asia/Shanghai')
name: CPU_ALL
time                      median_value
----                      ------------
1970-01-01T08:00:00+08:00 0.4
> 

SPREAD()最小值与最大值之间的数值差距

> select spread(*) from CPU_ALL tz('Asia/Shanghai')
name: CPU_ALL
time                      spread_value
----                      ------------
1970-01-01T08:00:00+08:00 100
> 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容