• 使用HTTP的API写入数据

    curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary '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 ~]#
    > 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参考文档。

    使用HTTP的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'

    上面的语句是写入三个数据点如下:

    [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 ~]#
    > 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字段。

    读取文件,然后使用HTTP的API来写入数据

    有时候可以直接根据日志文件的数据,写入InfluxDB中。

    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 ~]#

    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 ~]#
    > 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 数据是没有时间戳的,所以可以插入。其他都不能重复插入。

    无模式设计

    InfluxDB是一个无模式数据库。您可以随时添加新的测量,标签和字段。请注意,如果您尝试使用与以前使用的类型不同的类型编写数据(例如,将字符串写入先前接受整数的字段),InfluxDB将拒绝这些数据。

    HTTP响应摘要

    错误响应的示例:

    将浮点数据写入先前接受布尔值的字段:

    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\""}


    本文分享自微信公众号 - DevOps社群(DevOpsCommunity)。
    如有侵权,请联系 [email protected] 删除。
    本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

    09-10 07:31