1. HttpAPI
Endpoint | Description |
---|---|
/debug/requests | 测试某时间段的用户(client)query或write的情况。 |
/ping | 测试数据库状态,返回204则正常 |
/query | 查询的请求 |
/write | 写入数据的请求 |
$ curl http://localhost:8086/debug/requests?seconds=60
{
"user1:123.45.678.91": {"writes":3,"queries":0},
"user1:000.0.0.0": {"writes":0,"queries":16},
"user2:xx.xx.xxx.xxx": {"writes":4,"queries":0}
}
[root@pg02 influxdb]# curl -sl -I localhost:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 0cd8f6b3-2a3f-11eb-8015-525400a367e0
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0
X-Request-Id: 0cd8f6b3-2a3f-11eb-8015-525400a367e0
Date: Thu, 19 Nov 2020 08:13:05 GMT
$ curl http://localhost:8086/debug/requests?seconds=60
{
"user1:123.45.678.91": {"writes":3,"queries":0},
"user1:000.0.0.0": {"writes":0,"queries":16},
"user2:xx.xx.xxx.xxx": {"writes":4,"queries":0}
}
[root@pg02 influxdb]# curl -sl -I localhost:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 0cd8f6b3-2a3f-11eb-8015-525400a367e0
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0
X-Request-Id: 0cd8f6b3-2a3f-11eb-8015-525400a367e0
Date: Thu, 19 Nov 2020 08:13:05 GMT
1.1 query
GET http://localhost:8086/query
POST http://localhost:8086/query
GET http://localhost:8086/query
POST http://localhost:8086/query
Verb | Query Type |
---|---|
GET | 使用场景: SELECT , SHOW |
POST | 使用场景:SELECT INTO , ALTER , CREATE , DELETE , DROP , GRANT , KILL , REVOKE |
[root@pg02 influxdb]# curl -G 'http://localhost:8086/query?db=test' --data-urlencode 'q=SELECT * FROM "table"'
{"error":"unable to parse authentication credentials"}
[root@pg02 influxdb]# curl -G 'http://localhost:8086/query?db=test&u=admin&p=123456' --data-urlencode 'q=SELECT * FROM "table"'
{"results":[{"statement_id":0,"series":[{"name":"table","columns":["time","dd"],"values":[["2020-11-19T07:52:57.33605989Z",44]]}]}]}
$ curl -XPOST 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * INTO "newmeas" FROM "mymeas"'
$ curl -XPOST 'http://localhost:8086/query' --data-urlencode 'q=CREATE DATABASE "mydb"'
$ curl -XPOST 'http://localhost:8086/query?u=myusername&p=mypassword' --data-urlencode 'q=CREATE DATABASE "mydb"'
$ curl -G 'http://localhost:8086/query?db=mydb&epoch=s' --data-urlencode 'q=SELECT * FROM "mymeas";SELECT mean("myfield") FROM "mymeas"'
$ curl -H "Accept: application/csv" -G 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * FROM "mymeas"'
// 返回svg格式
name,tags,time,myfield,mytag1,mytag2
mymeas,,1488327378000000000,33.1,mytag1,mytag2
mymeas,,1488327438000000000,12.4,12,14
$ curl -G 'http://localhost:8086/query?db=mydb'
--data-urlencode 'q=SELECT * FROM "mymeas" WHERE "mytag1" = $tag_value AND "myfield" < $field_value'
--data-urlencode 'params={"tag_value":"12","field_value":30}'
[root@pg02 influxdb]# curl -G 'http://localhost:8086/query?db=test' --data-urlencode 'q=SELECT * FROM "table"'
{"error":"unable to parse authentication credentials"}
[root@pg02 influxdb]# curl -G 'http://localhost:8086/query?db=test&u=admin&p=123456' --data-urlencode 'q=SELECT * FROM "table"'
{"results":[{"statement_id":0,"series":[{"name":"table","columns":["time","dd"],"values":[["2020-11-19T07:52:57.33605989Z",44]]}]}]}
$ curl -XPOST 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * INTO "newmeas" FROM "mymeas"'
$ curl -XPOST 'http://localhost:8086/query' --data-urlencode 'q=CREATE DATABASE "mydb"'
$ curl -XPOST 'http://localhost:8086/query?u=myusername&p=mypassword' --data-urlencode 'q=CREATE DATABASE "mydb"'
$ curl -G 'http://localhost:8086/query?db=mydb&epoch=s' --data-urlencode 'q=SELECT * FROM "mymeas";SELECT mean("myfield") FROM "mymeas"'
$ curl -H "Accept: application/csv" -G 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * FROM "mymeas"'
// 返回svg格式
name,tags,time,myfield,mytag1,mytag2
mymeas,,1488327378000000000,33.1,mytag1,mytag2
mymeas,,1488327438000000000,12.4,12,14
$ curl -G 'http://localhost:8086/query?db=mydb'
--data-urlencode 'q=SELECT * FROM "mymeas" WHERE "mytag1" = $tag_value AND "myfield" < $field_value'
--data-urlencode 'params={"tag_value":"12","field_value":30}'
1.2 write
POST http://localhost:8086/write
POST http://localhost:8086/write
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary 'mymeas,mytag=1 myfield=90'
// 支持多行
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary 'mymeas,mytag=3 myfield=89
mymeas,mytag=2 myfield=34 1463689152000000000'
// 支持文件
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary @data.txt
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary 'mymeas,mytag=1 myfield=90'
// 支持多行
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary 'mymeas,mytag=3 myfield=89
mymeas,mytag=2 myfield=34 1463689152000000000'
// 支持文件
$ curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary @data.txt
1.3 开启鉴权后如何写数据
使用insert命令行写数据:
> INSERT cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000
> INSERT cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000
使用HTTP API写数据:
用户名密码写在 URL 中
curl -i -X POST "http://localhost:8086/write?db=mydb&u=admin&p=admin" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
curl -i -X POST "http://localhost:8086/write?db=mydb&u=admin&p=admin" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
用户名密码写在 HTTP 头 Authorization 选项
curl -i -X POST "http://localhost:8086/write?db=mydb" -u admin:admin --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
curl -i -X POST "http://localhost:8086/write?db=mydb" -u admin:admin --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
HTTP API 查询的方式变为
curl -G "http://localhost:8086/query" -u admin:admin --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query" --data-urlencode "u=admin" --data-urlencode "p=admin" --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query?u=admin&p=admin&q=SHOW+DATABASES"
curl -G "http://localhost:8086/query" -u admin:admin --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query" --data-urlencode "u=admin" --data-urlencode "p=admin" --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query?u=admin&p=admin&q=SHOW+DATABASES"
2. curl
2.1 write
- http api 写入
[root@TEST log]# curl -i -XPOST 'http://172.31.171.251:8086/write?db=test' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.74'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: c8362c7f-d858-11e9-804d-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.8
X-Request-Id: c8362c7f-d858-11e9-804d-0242ac110003
Date: Mon, 16 Sep 2019 08:05:43 GMT
[root@TEST log]# docker exec -it influxdb /bin/bash
root@a1229df1ff1e:/# influx
Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8
> use test
Using database test
> show measurements;
name: measurements
name
----
add_test
cpu_load_short
userBaseInfo
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1568621143922664620 server01 us-west 0.74
[root@TEST log]# curl -i -XPOST 'http://172.31.171.251:8086/write?db=test' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.74'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: c8362c7f-d858-11e9-804d-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.8
X-Request-Id: c8362c7f-d858-11e9-804d-0242ac110003
Date: Mon, 16 Sep 2019 08:05:43 GMT
[root@TEST log]# docker exec -it influxdb /bin/bash
root@a1229df1ff1e:/# influx
Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8
> use test
Using database test
> show measurements;
name: measurements
name
----
add_test
cpu_load_short
userBaseInfo
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1568621143922664620 server01 us-west 0.74
- 根据文件
cat txt
cpu_load_short,host=server01,region=us-west value=0.74
cpu_load_short,host=server02,region=us-west value=0.74
cpu_load_short,host=server03,region=us-west value=0.74
[root@TEST ~]# curl -i -XPOST 'http://172.31.171.251:8086/write?db=test' --data-binary @txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 353783c1-d859-11e9-8052-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.8
X-Request-Id: 353783c1-d859-11e9-8052-0242ac110003
Date: Mon, 16 Sep 2019 08:08:46 GMT
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1568621143922664620 server01 us-west 0.74
1568621326803092273 server01 us-west 0.74
1568621326803092273 server02 us-west 0.74
1568621326803092273 server03 us-west 0.74
cat txt
cpu_load_short,host=server01,region=us-west value=0.74
cpu_load_short,host=server02,region=us-west value=0.74
cpu_load_short,host=server03,region=us-west value=0.74
[root@TEST ~]# curl -i -XPOST 'http://172.31.171.251:8086/write?db=test' --data-binary @txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 353783c1-d859-11e9-8052-0242ac110003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.8
X-Request-Id: 353783c1-d859-11e9-8052-0242ac110003
Date: Mon, 16 Sep 2019 08:08:46 GMT
> select * from cpu_load_short
name: cpu_load_short
time host region value
---- ---- ------ -----
1568621143922664620 server01 us-west 0.74
1568621326803092273 server01 us-west 0.74
1568621326803092273 server02 us-west 0.74
1568621326803092273 server03 us-west 0.74
2.2 query
[root@bug ~]# curl -G 'http://172.31.171.251:8086/query?pretty=true' --data-urlencode "db=test" --data-urlencode "q=select value from cpu_load_short where region='us-west'"
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "cpu_load_short",
"columns": [
"time",
"value"
],
"values": [
[
"2019-09-16T08:05:43.92266462Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
]
]
}
]
}
]
}
[root@bug ~]# curl -G 'http://172.31.171.251:8086/query?pretty=true' --data-urlencode "db=test" --data-urlencode "q=select value from cpu_load_short where region='us-west'"
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "cpu_load_short",
"columns": [
"time",
"value"
],
"values": [
[
"2019-09-16T08:05:43.92266462Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
],
[
"2019-09-16T08:08:46.803092273Z",
0.74
]
]
}
]
}
]
}
2.3带有用户查询
查询
http带上admin用户查询、写入数据
curl -G "http://localhost:8086/query" -u username:password --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query" --data-urlencode "u=username" --data-urlencode "p=password" --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query?u=username&p=password&q=SHOW+DATABASES"
curl -G "http://localhost:8086/query" -u username:password --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query" --data-urlencode "u=username" --data-urlencode "p=password" --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query?u=username&p=password&q=SHOW+DATABASES"
- json展开显示
?pretty=true
能够将json展开显示
curl -G "http://localhost:8086/query?pretty=true" -u root:newpwd --data-urlencode "q=show databases"
curl -G "http://localhost:8086/query?pretty=true" -u root:newpwd --data-urlencode "q=show databases"
写入
用户名密码写在URL中
curl -i -X POST "http://localhost:8086/write?db=mydb&u=username&p=password" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
curl -i -X POST "http://localhost:8086/write?db=mydb&u=username&p=password" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
用户名密码写在HTTP头Authorization选项
curl -i -X POST "http://localhost:8086/write?db=mydb" -u username:password --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
curl -i -X POST "http://localhost:8086/write?db=mydb" -u username:password --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"