默认情况下,如果所有数据存储成功,响应一个204的状态码。如果有一个或多个数据点出错,返回400状态码和错误消息内容。
curl -i -X POST -d '{"metric":"press", "timestamp":1603270945, "value":6, "tags": {"host":"web02"}}' http://localhost:4242/api/put?summary或者
curl -i -X POST -d '{"metric":"press", "timestamp":1603270941, "value":6, "tags": {"host":"web02"}}' http://localhost:4242/api/put?details或者
curl -i -X POST -d '{"metric":"press", "timestamp":1603270948, "value":6, "tags": {"host":"10.10.10.10"}}' http://localhost:4242/api/put?summary-i参数打印出服务器回应的 HTTP 标头。 -X参数指定 HTTP 请求的方法。 -d参数用于发送 POST 请求的数据体。
项目中基于python request包实现HTTP API接口,并进行封装写入。代码如下:这里其实是HTTP API的批量写入实现。
import requests data1 = { "metric": "press", "timestamp": '1603271000', "value": '129', "tags": { "host": "web01", "name": "motor" } } data2 = { "metric": "press", "timestamp": '1603272000', "value": '330', "tags": { "host": "web01", "name": "motor" } } datalist = [data1,data2] if __name__ == "__main__": res = requests.post("http://10.23.241.205:4242/api/put?details", json=datalist) print(res.text)输出 {“success”:2,“failed”:0,“errors”:[]}
输出如下
{'metric': 'press', 'tags': {'host': 'web01', 'dc': 'lga'}, 'aggregateTags': [], 'dps': {'1603274787': 10.19815731048584, '1603274788': 9.282279968261719, '1603274789': 9.026270866394043}} {'1603274787': 10.19815731048584, '1603274788': 9.282279968261719, '1603274789': 9.026270866394043}(1)写入
import requests data1 = { "metric":"temperature", "timestamp":1567675709879, "value":20, "tags":{ "host":"web1" } } data2 = { "metric":"temperature", "timestamp":1567675709900, "value":60, "tags":{ "host":"web1" } } datalist = [data1,data2] if __name__ == "__main__": res = requests.post("http://10.23.241.205:4242/api/put?details", json=datalist) print(res.text)(2)查询 To extract data with millisecond resolution, use the /api/query endpoint and specify the msResolution (ms is also okay, but not recommended) JSON parameter or query string flag and it will bypass down sampling (unless specified) and return all timestamps in Unix epoch millisecond resolution.
# -*- coding: utf-8 -*- import requests if __name__ == "__main__": query = 'start=1567675709879&ms&m=sum:temperature' result = requests.get("http://10.23.241.205:4242/api/query?" + query) print(result.json()[0]) print(result.json()[0]['dps'])输出显示
{'metric': 'temperature', 'tags': {'host': 'web1'}, 'aggregateTags': [], 'dps': {'1567675709879': 20, '1567675709900': 60}} {'1567675709879': 20, '1567675709900': 60}