Influxdb使用Http请求查询近6个小时数据的简单示例

influxdb提供的http api进行数据访问,但是官方注明http访问是一种初级的数据操作方式,项目上还是建议是用客户端程序和第三方库进行数据操作。

http插入数据示例

curl -i -XPOST "http://127.0.0.1:8086/write?db=server_monitor" --data-binary "sys_cpu,host=${hostip},server=${hostname} value=${cpu_load}"

这个是向server_monitor这个库的sys_cpu表中插入一条数据的示例

influxdb http查询数据示例

curl -GET 'http://127.0.0.1:8086/query?pretty=true' --data-urlencode "db=iotbigdata" --data-urlencode "chunk_size=5" --data-urlencode "epoch=ms" --data-urlencode "q=SELECT value FROM iotelectricity WHERE param='voltage' AND time>now()-6h GROUP BY deviceSn"

chunk_size:指定返回数据result结果的大小

epoch:设置输出的时间格式,epoch=[h,m,s,ms,u,ns]

time:可以设置数据返回时间范围,例如过去6小时的数据,time>now()-6h

转换成浏览器可访问的http链接:

http://127.0.0.1:8086/query?pretty=true&db=iotbigdata&chunk_size=5&epoch=ms&q=SELECT%20"value"%20FROM%20"iotelectricity"%20WHERE%20("param"%20%3D%20%27voltage%27)%20AND%20time%20>%3D%20now()%20-%206h%20GROUP%20BY%20"deviceSn"

返回数据格式示例:

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "iotelectricity",
                    "tags": {
                        "deviceSn": "890000002872"
                    },
                    "columns": [
                        "time",
                        "value"
                    ],
                    "values": [
                        [
                            1588727335083,
                            "230"
                        ],
                        [
                            1588729135100,
                            "230"
                        ],
                        [
                            1588730935021,
                            "228"
                        ],
                        [
                            1588732735047,
                            "230"
                        ],
                        [
                            1588734535056,
                            "230"
                        ],
                        [
                            1588737549045,
                            "231"
                        ],
                        [
                            1588737752827,
                            "231"
                        ],
                        [
                            1588738134973,
                            "231"
                        ]
                    ]
                },
                {
                    "name": "iotelectricity",
                    "tags": {
                        "deviceSn": "890000002881"
                    },
                    "columns": [
                        "time",
                        "value"
                    ],
                    "values": [
                        [
                            1588727335018,
                            "229"
                        ],
                        [
                            1588729135002,
                            "229"
                        ],
                        [
                            1588730934958,
                            "228"
                        ],
                        [
                            1588732734979,
                            "230"
                        ],
                        [
                            1588734535006,
                            "230"
                        ],
                        [
                            1588738134910,
                            "231"
                        ]
                    ]
                },
                {
                    "name": "iotelectricity",
                    "tags": {
                        "deviceSn": "890000002882"
                    },
                    "columns": [
                        "time",
                        "value"
                    ],
                    "values": [
                        [
                            1588727335122,
                            "229"
                        ],
                        [
                            1588729135154,
                            "230"
                        ],
                        [
                            1588730935057,
                            "228"
                        ],
                        [
                            1588732735081,
                            "230"
                        ],
                        [
                            1588734535087,
                            "230"
                        ],
                        [
                            1588737550974,
                            "231"
                        ],
                        [
                            1588737552982,
                            "231"
                        ],
                        [
                            1588737555040,
                            "231"
                        ],
                        [
                            1588737556982,
                            "231"
                        ],
                        [
                            1588737559006,
                            "231"
                        ],
                        [
                            1588737561046,
                            "231"
                        ],
                        [
                            1588737563003,
                            "231"
                        ],
                        [
                            1588737754816,
                            "231"
                        ],
                        [
                            1588737756823,
                            "230"
                        ],
                        [
                            1588737758825,
                            "230"
                        ],
                        [
                            1588737760808,
                            "230"
                        ],
                        [
                            1588738135005,
                            "231"
                        ]
                    ]
                }
            ]
        }
    ]
}

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注