• 周六. 8月 20th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

influxdb[2]-读写数据

admin

11月 28, 2021

InfluxDB提供多种数据写入方式,包括命令行、客户端、插件等。Cli写入数据已在上文中描述,本文主要介绍使用HTTP接口读写数据。

使用HTTP接口创建数据库

接口地址:/query

POST,参数qCREATE DATABASE <new_database_name>,示例:

curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb1"

HTTP/1.1 200 OK
Content-Type: application/json
Request-Id: 658c7410-ea97-11eb-8030-525400d9065e
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.6
X-Request-Id: 658c7410-ea97-11eb-8030-525400d9065e
Date: Thu, 22 Jul 2021 02:49:12 GMT
Transfer-Encoding: chunked

{"results":[{"statement_id":0}]}

写单条数据

接口地址:/write,具体格式如下:

curl -i -XPOST 'http://localhost:8086/write?db=mydb1' --data-binary 'cpu,host=server01,region=us-west value=0.69 1626858254189591692'

HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: f606e8f7-ea97-11eb-8031-525400d9065e
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.6
X-Request-Id: f606e8f7-ea97-11eb-8031-525400d9065e
Date: Thu, 22 Jul 2021 02:53:15 GMT

HTTP 204 No Content`,说明写入成功。

批量写入

curl -i -XPOST 'http://localhost:8086/write?db=mydb1' --data-binary 'cpu,host=server02 value=0.67
cpu,host=server02,region=us-west value=0.55 1626858254189591692
cpu,host=server03,region=us-west value=2.0 1626858254189591692'

HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: e9c4ad08-ea98-11eb-8032-525400d9065e
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.6
X-Request-Id: e9c4ad08-ea98-11eb-8032-525400d9065e
Date: Thu, 22 Jul 2021 03:00:04 GMT

批量写入文件

curl -i -XPOST 'http://localhost:8086/write?db=mydb1' --data-binary @cpu_data.txt

给一个正确的文件(cpu_data.txt)的例子,数据的格式需要满足InfluxDB行格式:

cpu,host=server02 value=0.67
cpu,host=server02,region=us-west value=0.55 1626858254189591692
cpu,direction=in,host=server01,region=us-west value=2.0 1626858254189591692

查询数据

接口地址:/query

curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb1" --data-urlencode "q=SELECT * FROM "cpu" WHERE "region"='us-west'"

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "cpu",
                    "columns": [
                        "time",
                        "host",
                        "region",
                        "value"
                    ],
                    "values": [
                        [
                            "2021-07-21T09:04:14.189591692Z",
                            "server02",
                            "us-west",
                            0.55
                        ],
                        [
                            "2021-07-21T09:04:14.189591692Z",
                            "server03",
                            "us-west",
                            2
                        ],
                        [
                            "2021-07-22T02:53:15.355312874Z",
                            "server01",
                            "us-west",
                            0.69
                        ]
                    ]
                }
            ]
        }
    ]
}

多个数据

接口地址:/query

在一次API调用中发送多个InfluxDB的查询语句,可以简单地使用分号分隔每个查询

curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb1" --data-urlencode "q=SELECT "value" FROM "cpu" WHERE "region"='us-west';SELECT count("value") FROM "cpu" WHERE "region"='us-west'"

其它参数

时间戳格式

在InfluxDB中的所有数据都是存的UTC时间,时间戳默认返回RFC3339格式的纳米级的UTC时间,例如2021-07-22T02:53:15.355312874Z,如果你想要返回Unix格式的时间,可以在请求参数里设置epoch参数,其中epoch可以是[h,m,s,ms,u,ns]之一。例如返回一个秒级的epoch:

curl -G 'http://localhost:8086/query?pretty=true'  --data-urlencode "epoch=s" --data-urlencode "db=mydb1" --data-urlencode "q=SELECT * FROM "cpu" WHERE "region"='us-west'"

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "cpu",
                    "columns": [
                        "time",
                        "host",
                        "region",
                        "value"
                    ],
                    "values": [
                        [
                            1626858254,
                            "server02",
                            "us-west",
                            0.55
                        ],
                        [
                            1626858254,
                            "server03",
                            "us-west",
                            2
                        ],
                        [
                            1626922395,
                            "server01",
                            "us-west",
                            0.69
                        ]
                    ]
                }
            ]
        }
    ]
}

最大行限制

分块(chunking)

HTTP返回值概要

  • 2xx:如果写数据后收到HTTP 204 No Content,说明写入成功
  • 4xx:表示InfluxDB不知道你发的是什么。
  • 5xx:系统过载或是应用受损。

发表回复

您的电子邮箱地址不会被公开。