仅供学习,转载请注明出处
Centos7 下 InfluxDB 从安装开始到入门
InfluxDB关键概念
经过前面两个篇章的探讨,基本已经了解了InfluxDB的操作,下面再来继续看看使用HTTP API编写数据。
首先查看InfluxDB当前有哪些数据库了。
[root@server81 ~]# docker exec -it influxdb bash
root@d2918dc47850:/#
root@d2918dc47850:/# influx
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
>
> show databases;
name: databases
name
----
_internal
mydb
>
可以从上面看出,当前有 _internale
和 mydb
两个数据库。
下面使用POST请求创建一个
testdb
数据库
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"
执行如下:
## 查看一下influxdb的进程
[root@server81 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2918dc47850 influxdb "/entrypoint.sh in..." 2 days ago Up 2 days 0.0.0.0:8086->8086/tcp influxdb
82a294241ff7 registry:2 "/entrypoint.sh /e..." 4 weeks ago Up 5 days 0.0.0.0:5000->5000/tcp registry
[root@server81 ~]#
## 使用http的api创建数据库testdb
[root@server81 ~]# curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"
HTTP/1.1 200 OK
Content-Type: application/json
Request-Id: 41f2a1c7-1250-11e9-8071-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: 41f2a1c7-1250-11e9-8071-0242ac110003
Date: Mon, 07 Jan 2019 07:45:52 GMT
Transfer-Encoding: chunked
{"results":[{"statement_id":0}]}
[root@server81 ~]#
执行完毕之后,查看一下数据库是否创建成功,如下:
## 查看一下所有influxdb的数据库,可以看到已经创建好了testdb了。
> show databases;
name: databases
name
----
_internal
mydb
testdb
>
> use testdb
Using database testdb
>
## 查看testdb有哪些表
> show measurements
>
>
那么在这里再次分析一下这个请求的格式:
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"
格式说明:
/query
curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
请求格式说明:
/write?db=testdb
,说明将数据写入testdb
数据库中。--data-binary
说明 HTTP POST请求中的数据为纯二进制数据'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
cpu_load_short
表中host=server01,region=us-west
value=0.64
1434055562000000000
,如果不插入时间,则会写入服务器的本地时间执行如下:
## 执行API请求插入数据
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: a9f092e4-1254-11e9-8075-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: a9f092e4-1254-11e9-8075-0242ac110003
Date: Mon, 07 Jan 2019 08:17:24 GMT
[root@server81 ~]#
执行请求之后,到InfluxDB数据库中查询看看:
> show measurements
name: measurements
name
----
cpu_load_short
>
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1434055562000000000 server01 us-west 0.64
>
写入点时,必须指定现有的数据库。有关可用查询参数的完整列表,请参阅API参考文档。
curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
上面的语句是写入三个数据点如下:
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
先执行请求来看看是什么效果
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server02 value=0.67
> cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
> cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: f3b56186-1255-11e9-8079-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: f3b56186-1255-11e9-8079-0242ac110003
Date: Mon, 07 Jan 2019 08:26:38 GMT
[root@server81 ~]#
插入数据之后,在InfluxDB查询看看
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1434055562000000000 server01 us-west 0.64
>
> select * from cpu_load_short
name: cpu_load_short
time direction host region value
---- --------- ---- ------ -----
1422568543702900257 in server01 us-west 2
1422568543702900257 server02 us-west 0.55
1434055562000000000 server01 us-west 0.64
1546849598178339889 server02 0.67
>
好了,从上面可以看出,新插入了三条数据,并且cpu_load_short
表还自动增加了一个direction
字段。
有时候可以直接根据日志文件的数据,写入InfluxDB中。
首先准备好一个文件
cpu_data.txt
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
注意:该文件内的格式应该遵循InfluxDB的行协议语法。
准备文件如下:
[root@server81 ~]# cat cpu_data.txt
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
[root@server81 ~]#
执行API写入数据
curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txt
执行如下:
## 查看文件内容数据
[root@server81 ~]# cat cpu_data.txt
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
[root@server81 ~]#
## 读取文件,然后使用API请求写入InfluxDB
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003
Date: Mon, 07 Jan 2019 08:36:15 GMT
[root@server81 ~]#
查看InfluxDB:
> select * from cpu_load_short
name: cpu_load_short
time direction host region value
---- --------- ---- ------ -----
1422568543702900257 in server01 us-west 2
1422568543702900257 server02 us-west 0.55
1434055562000000000 server01 us-west 0.64
1546849598178339889 server02 0.67
>
> select * from cpu_load_short
name: cpu_load_short
time direction host region value
---- --------- ---- ------ -----
1422568543702900257 in server01 us-west 2
1422568543702900257 server02 us-west 0.55
1434055562000000000 server01 us-west 0.64
1546849598178339889 server02 0.67
1546850175491084332 server02 0.67
>
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
可以看到这条数据在插入之前已经有了的。但是查询数据并没有重复插入,说明只要数据完全一致,并不会重复插入。
下面来重复插入测试一下看看:
## 前面的第一次数据插入
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003
Date: Mon, 07 Jan 2019 08:36:15 GMT
[root@server81 ~]#
## 第二次数据插入
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: f5eb3016-1257-11e9-807d-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: f5eb3016-1257-11e9-807d-0242ac110003
Date: Mon, 07 Jan 2019 08:41:00 GMT
[root@server81 ~]#
查看重复插入同样数据后的InfluxDB
> select * from cpu_load_short
name: cpu_load_short
time direction host region value
---- --------- ---- ------ -----
1422568543702900257 in server01 us-west 2
1422568543702900257 server02 us-west 0.55
1434055562000000000 server01 us-west 0.64
1546849598178339889 server02 0.67
1546850175491084332 server02 0.67
>
> select * from cpu_load_short
name: cpu_load_short
time direction host region value
---- --------- ---- ------ -----
1422568543702900257 in server01 us-west 2
1422568543702900257 server02 us-west 0.55
1434055562000000000 server01 us-west 0.64
1546849598178339889 server02 0.67
1546850175491084332 server02 0.67
1546850460880063366 server02 0.67
>
因为插入的数据中,只有这条cpu_load_short,host=server02 value=0.67
数据是没有时间戳的,所以可以插入。其他都不能重复插入。
注意:如果您的数据文件有超过5,000个点,则可能需要将该文件拆分为多个文件,以便将数据批量写入InfluxDB。默认情况下,HTTP请求在五秒后超时。在此之后,InfluxDB仍会尝试写点数据,但不会确认它们是否已成功编写。
InfluxDB是一个无模式数据库。您可以随时添加新的测量,标签和字段。请注意,如果您尝试使用与以前使用的类型不同的类型编写数据(例如,将字符串写入先前接受整数的字段),InfluxDB将拒绝这些数据。
将浮点数据写入先前接受布尔值的字段:
curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=true'
curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=5'
执行如下:
## 首先写入新表tobeornottobe一个布尔值的字段,写入成功
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=true'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 34ff0315-1259-11e9-807f-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.2
X-Request-Id: 34ff0315-1259-11e9-807f-0242ac110003
Date: Mon, 07 Jan 2019 08:49:56 GMT
[root@server81 ~]#
## 将数值5写入布尔值的字段,则400报错
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=5'
HTTP/1.1 400 Bad Request
Content-Type: application/json
Request-Id: 3ab628e4-1259-11e9-8080-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Error: partial write: field type conflict: input field "booleanonly" on measurement "tobeornottobe" is type float, already exists as type boolean dropped=1
X-Influxdb-Version: 1.7.2
X-Request-Id: 3ab628e4-1259-11e9-8080-0242ac110003
Date: Mon, 07 Jan 2019 08:50:05 GMT
Content-Length: 165
{"error":"partial write: field type conflict: input field \"booleanonly\" on measurement \"tobeornottobe\" is type float, already exists as type boolean dropped=1"}
[root@server81 ~]#
查看一下InfluxDB的数据:
> show measurements
name: measurements
name
----
cpu_load_short
tobeornottobe
>
> select * from tobeornottobe
name: tobeornottobe
time booleanonly
---- -----------
1546850996203127571 true
>
可以看出,数据只插入了一条。
那么再来看看,如果将数据写入一个不存在的数据库,会报什么错误呢?
[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=atlantis' --data-binary 'liters value=10'
HTTP/1.1 404 Not Found
Content-Type: application/json
Request-Id: c8c48879-1259-11e9-8083-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Error: database not found: "atlantis"
X-Influxdb-Version: 1.7.2
X-Request-Id: c8c48879-1259-11e9-8083-0242ac110003
Date: Mon, 07 Jan 2019 08:54:04 GMT
Content-Length: 45
{"error":"database not found: \"atlantis\""}
[root@server81 ~]#
从上面可以看到,报了数据库找不到的错误{"error":"database not found: \"atlantis\""}
。
关注微信公众号,回复【资料】、Python、PHP、JAVA、web,则可获得Python、PHP、JAVA、前端等视频资料。