总结下influxdb cli操作方式的内容。
$ influxd version InfluxDB v1.4.2 (git: 1.4 6d2685d1738277a1c2672fc58df7994627769be6)
本文内容均是基于InfluxDB v1.4.2版本进行实践的!
连接influxdb
$ influx -precision rfc3339
指定time时间戳展示形式RFC3339
influxdb查询数据库
show databases
> show databases; name: databases name ---- _internal visualize visualize_day visualize_minutes
influxdb使用某个库
use <database_name>
> use visualize_minutes; Using database visualize_minutes
指定当前使用库的目的是为了保证后续measurement(表)的操作,当然可以不指定,如在select后跟条件on指定库名,也可以,不指定的话,默认当前use指定的库。如果就某一库内操作,建议最好指定。
influxdb查看measurements
show measurements
列出当前库(未use指定情况下,默认库)下的所有measurement(表)。
> show measurements; name: measurements name ---- video user device
删除指定measurement
删除当前库中的某个measurement:drop measurement <measurement_name>
如删除当前库中test表:
> show measurements; name: measurements name ---- test video > drop measurement test; > show measurements; name: measurements name ---- video
查看某个measurement其所有field key
show field keys from <measurement_name>
如下,查看test表的所有field key有哪些:
> show field keys from test; name: test fieldKey fieldType -------- --------- age integer sex string
查看某个measurement其所有tag key
show tag keys from <measurement_name>
如下,查看test表的所有tag key有哪些:
> show tag keys from test; name: test tagKey ------ source
influxdb插入数据
- 基本语法模板:
insert + measurement + "," + tag=value,tag=value + " " + field=value,field=value
- tag与tag之间用逗号分隔;field与field之间用逗号分隔;
- tag与field之间用空格分隔;
- tag都是string类型,不需要引号将value包裹;
- field如果是string类型,需要加引号。如果是其他类型,也需要显示指定,如果不指定,则默认integer;具体类型说明,见下文。
- 可以省略不插入某个tag字段值或field字段值。
当measurement不存在的时候,我们插入一条数据时,会创建这个measurement。
> show measurements; name: measurements name ---- video user device > insert test,source=5 age=24i,sex="man",money=100; > show measurements; name: measurements name ---- device test user video
可以看到,一开始没有test表,当插入一条数据后,生成了test表。
influxdb field支持四种数据类型:
类型 | 方式 | 实例 |
float | 数字 | money=100 |
integer | 数字i | age=24i |
boolean | true/false | student=false |
string | “” 或 ” | sex=”man” |
查看刚刚创建的test表field keys
> show field keys from test; name: test fieldKey fieldType -------- --------- age integer money float sex string > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5
如果field的值类型为string,但却没有带引号,则会报错,如下这里sex=man
,man是string类型值。
> insert test,source="5" age=24i,sex=man,money=100 ERR: {"error":"unable to parse 'test,source="5" age=24i,sex=man,money=100': invalid boolean"}
需要注意!tag只有string类型,所以不用带引号,如果带了引号,则认为引号也是值的一部分,需要特别注意,如下:
> insert test,source="5" age=24i,sex="man",money=100 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5"
指定时间戳
当写入数据不指定时间时,会自动用当前时间来补齐。如果需要指定时间,在最后面添加上即可,注意时间为世界时ns(纳秒)
> insert test,source=6 age=25i,sex="man",money=100 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6
可以省略插入某个tag字段值或field字段值
不显示指定对应字段即可,如这里不指定source tag,且不指定sex field。
> insert test age=25i,money=100 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 1627890150830158873 25 100 man 6
关于insert的特别说明
1、必须严格按照insert模板格式
注意空格以及‘,’,必须严格按照上面声明的模板格式。如下,表名test和tag “source”之间多写了空格,则执行错误。
> insert test, source=8 age=26,money=110,sex="" ERR: {"error":"unable to parse 'test, source=8 age=26,money=110,sex=""': missing tag key"}
2、阴差阳错的insert
如果没有按照insert模板顺序那样书写,会造成意想不到的结果。如下:
实际,是想要插入一条tag:source=7,且field:age=26i,但确是这样写的
> insert test source=8,age=26i > select * from test; name: test time age money sex source source_1 ---- --- ----- --- ------ -------- 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7 1627892081122296401 26 8
你可能会纳闷上面select的结果,为什么会有一个source_1??不妨插入后,再来看下test表的tag keys和field keys。
> show tag keys from test; name: test tagKey ------ source > show field keys from test; name: test fieldKey fieldType -------- --------- age integer money float sex string source float
其实insert test source=8,age=26i
这句意思是插入一条记录,指定两个filed key:source为8,同时age为26i,time为当前时间的数据。所以在原有表的基础上,又创建了一个“source” field。但select *
的时候,是将所有field和tag一并展示,因为同时存在命名为“source”的field和tag,所以select *
的时候就做了兼容处理,_数字的这种方式展示!这里要只想展示source tag和其他field,可以指明命名冲突字段的key类型,是tag还是field,如下:
> select age,money,sex,source::tag from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7 1627892081122296401 26
关于select语法可以详见下文。
上面这个例子说明,在编写insert的时候,一定要严格按照模板规范来写!
3、插入新数据,field key新值类型与原始类型不一致,则会报错。
test表的“age” field_key本来是integer类型,但下面两个insert却插入的是非integer类型的“age”。
> insert test age=26,money=110,sex="" 1627890150830158873 ERR: {"error":"partial write: field type conflict: input field "age" on measurement "test" is type float, already exists as type integer dropped=1"} > insert test age="26",money=110,sex="" 1627890150830158873 ERR: {"error":"partial write: field type conflict: input field "age" on measurement "test" is type string, already exists as type integer dropped=1"}
更新某条数据记录
influxdb不支持像mysql等数据库那种update,但可以通过重复插入的逻辑,对值的覆写,来实现更新。
但需注意,当需要更新更新某个field key值时,需要保证tag key和time时间戳保持不变,这样influxdb才知道是在同一个维度的更新操作,否则将变成插入一条新的数据。实践如下:
我想对time=1627890150830158873且source为空的这条记录,更新其money字段100为110。
> select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 1627890150830158873 25 100 man 6 > insert test age=25i,money=110 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 110 1627890150830158873 25 100 man 6 > insert test age=25i,money=110,sex="" 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 110 1627890150830158873 25 100 man 6 > insert test,source=7 age=25i,money=110,sex="" 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 25 110 1627890150830158873 25 110 7 > insert test age=26i,money=110,sex="" 1627890150830158873 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7
insert test age=25i,money=110,sex="" 1627890150830158873
和insert test age=25i,money=110 1627890150830158873
是等价的,都是指定sex为””,值为空,省略字段即默认值,string默认空,所以等价。同时和insert test age=26i,money=110,sex="" 1627890150830158873
这三条语句都是指向同一条记录,因为都是指定了source空且时间戳time为1627890150830158873。但如果指定source=7,即insert test,source=7 age=25i,money=110,sex="" 1627890150830158873
就是插入一条新数据了,和原来source为空,并不是一个维度,同时表中原本并不存在source=7且time=1627890150830158873的数据,所以不会更新旧数据。
select查询
现表如下
> insert test,source=2 age=24i,sex="man",source="a" > select * from test; name: test time age sex source source_1 ---- --- --- ------ -------- 1627898618544625549 24 man a 2 > show tag keys from test; name: test tagKey ------ source > show field keys from test; name: test fieldKey fieldType -------- --------- age integer sex string source string
基本select
select所有field和tag key
select *
> select * from test; name: test time age sex source source_1 ---- --- --- ------ -------- 1627898618544625549 24 man a 2
注意,如果存在命名冲突的字段,select *
的时候会按下划线数字处理。
select所有field
select *::field
> select *::field from test; name: test time age sex source ---- --- --- ------ 1627898618544625549 24 man a
不支持select所有tag,即select *::tag
没有效果!!
> select *::tag from test; >
查询部分field字段
select "<field_key>","<field_key>"
或显示带field关键字select "<field_key>"::field,<field_key>"::field
> select sex,age,source from test; name: test time sex age source ---- --- --- ------ 1627898618544625549 man 24 a > select sex::field,age::field,source::field from test; name: test time sex age source ---- --- --- ------ 1627898618544625549 man 24 a
查询指定field或tag
select "<field_key>"::field,"<tag_key>"::tag
> select sex::field,age::field,source::tag from test; name: test time sex age source ---- --- --- ------ 1627898618544625549 man 24 2
这种方式,尤其在field和tag存在命名冲突的表中查询其tag时,受用。
删除measurement中的列
influxdb暂不支持删除某一列,但可以通过select into
临时中间表的方式,将不需要删除的列导出临时表,然后将删除原表,在将临时表写出到“原表名”的表。
现在有这么一个表
> select * from test; name: test time age money sex source source_1 ---- --- ----- --- ------ -------- 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7 1627892081122296401 26 8 > show tag keys from test; name: test tagKey ------ source > show field keys from test; name: test fieldKey fieldType -------- --------- age integer money float sex string source float
我想删除field:source,这个是阴差阳错创建出来的错误字段。于是,这里过滤掉field “source”,显示指定tag “source”,中间临时表为“tmp”。
> select age,money,sex,source::tag into tmp from test; name: result time written ---- ------- 0 6 > select * from tmp; name: tmp time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 110 7 1627892081122296401 26
看似结果可以了,但实际存在问题,schema存在问题。
> show tag keys from tmp; > show field keys from tmp; name: tmp fieldKey fieldType -------- --------- age integer money float sex string source string
直接select into
,influxdb会认为select的字段都作为field写入临时表。而我们想要的是,“source”作为tag key。
如果需要将原始表的tag key保持不变写入临时表,则需要group by配合完成。可以group by后跟需要保持不变的tag key,这里因为我们原始表只有一个tag key,就是“source”,所以可以直接group by *
,当然也可以group by source
(关于select group by的语法可以参考select语法章节)。具体如下:
> select age,money,sex into tmp from test group by *; name: result time written ---- ------- 0 6 > select * from tmp; name: tmp time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7 1627892081122296401 26 > show field keys from tmp; name: tmp fieldKey fieldType -------- --------- age integer money float sex string > show tag keys from test; name: test tagKey ------ source > drop measurement test; > select *::field into test from tmp group by *; name: result time written ---- ------- 0 6 > select * from test; name: test time age money sex source ---- --- ----- --- ------ 1627886526606090135 24 100 man 5 1627889906757120835 24 100 man "5" 1627890150830158873 25 100 man 6 1627890150830158873 26 110 1627890150830158873 25 110 7 1627892081122296401 26 > show tag keys from test; name: test tagKey ------ source > show field keys from test; name: test fieldKey fieldType -------- --------- age integer money float sex string > drop measurement tmp;
注意,select into from group by
的时候,select的内容不要再写tag key,否则会冲突。如下:
> select age,money,sex,source::tag into tmp from test group by *; name: result time written ---- ------- 0 6 > select * from tmp; name: tmp time age money sex source source_1 ---- --- ----- --- ------ -------- 1627886526606090135 24 100 man 5 5 1627889906757120835 24 100 man "5" "5" 1627890150830158873 25 100 man 6 6 1627890150830158873 26 110 1627890150830158873 25 110 7 7 1627892081122296401 26 > show field keys from tmp; name: tmp fieldKey fieldType -------- --------- age integer money float sex string source string > show tag keys from test; name: test tagKey ------ source
当然,如果你要删除的列包含了tag,即要保持的列仅为field,则不用group by,直接select into
。
本文参考
- InfluxDB中文文档:https://jasper-zhang1.gitbooks.io/influxdb/content/
- Influx Sql系列教程五:insert 添加数据:https://juejin.cn/post/6844903920528162824
- influxdb删除某一列(tag或者field):https://blog.csdn.net/weixin_43135178/article/details/108844020
发表评论