前言
上篇文章介绍了opentsdb的四种写入方式,但只展示了其中三种的使用案例,分别是:Telnet Put、cli import、Tcollector。HTTP API接口在上篇博客中只对官网进行翻译介绍,但该接口是比较常用的方式,而且基于HTTP API的实现方式较多,所以本篇博客单独进行说明展示。
1. Postman 写入
postman 是一款模拟网页HTTP请求的插件,详细说明不在此介绍。
使用展示:
代码示例:
http://localhost:4242/api/put?summary
[
{
"metric": "sys.cpu.nice",
"timestamp": 1483399261,
"value": 18,
"tags": {
"host": "web01",
"dc": "lga"
}
},
{
"metric": "sys.cpu.nice",
"timestamp": 1483399262,
"value": 26,
"tags": {
"host": "web02",
"dc": "lga"
}
},
{
"metric": "sys.cpu.nice",
"timestamp": 1483399263,
"value": 16,
"tags": {
"host": "web01",
"dc": "lga"
}
},
{
"metric": "sys.cpu.nice",
"timestamp": 1483399264,
"value": 16,
"tags": {
"host": "web01",
"dc": "lga"
}
}
]
结果展示:
2. 终端写入
使用mac os终端
参考:http://www.ttlsa.com/opentsdb/opentsdb-insert-data-http-api-interface/
默认情况下,如果所有数据存储成功,响应一个204的状态码。如果有一个或多个数据点出错,返回400状态码和错误消息内容。
curl -i -X POST –d '{"metric":"proc.loadavg.15m","timestamp":1483399261,"value":6, "tags":
{"host":"10.0.101.145"}}' http://localhost:4242/api/put?summary
curl -i -X POST -d '{"metric":"proc.loadavg.15m", "timestamp":1483399262, "value":8, "tags": {"host":"10.0.101.145"}}' http://localhost:4242/api/put?details
结果如下图:
3. Python Request写入(短连接)
项目中基于python request包实现HTTP API接口,并进行封装写入。代码如下:
这里其实是HTTP API的批量写入实现。参考官方资料 Example Multiple Data Point Put小节
import requests
payload = {
"metric": "sys.cpu.nice",
"timestamp": '1489544891',
"value": '29',
"tags": {
"host": "web01",
"dc": "lga"
}
}
payload1 = {
"metric": "sys.cpu.nice",
"timestamp": '1489544892',
"value": '30',
"tags": {
"host": "web01",
"dc": "lga"
}
}
payload2 = {
"metric": "sys.cpu.nice",
"timestamp": '1489544893',
"value": '29',
"tags": {
"host": "web01",
"dc": "lga"
}
}
payload3 = {
"metric": "sys.cpu.nice",
"timestamp": '1489544894',
"value": '30',
"tags": {
"host": "web01",
"dc": "lga"
}
}
ls = [payload, payload1, payload2, payload3]
def send_json(json):
r = requests.post("http://localhost:4242/api/put?details", json=json)
return r.text
def main():
print send_json(ls)
if __name__ == "__main__":
main()
结果如下图:
4. Python Request写入(长连接)
第四种方式的写入是第三种方式的改进,10w条数据,写入性能快了1.5倍左右。
import time
import math
import requests
def get_value(num):
return math.sin(num)+1
def send_json(json, s):
r = s.post("http://localhost:4242/api/put?details", json=json)
return r.text
def main():
s = requests.Session()
a = int(time.time()) - 100000
ls = []
for i in range(1, 100000):
json = {
"metric": "sys.batch.test6",
"timestamp": a,
"value": get_value(i),
"tags": {
"host": "web01",
"dc": "lga"
}
}
i += 0.01
a += 1
ls.append(json)
if len(ls) == 50:
send_json(ls, s)
ls = []
send_json(ls, s)
ls = []
if __name__ == "__main__":
start = time.time()
main()
print time.time()-start
5. 测试对比
这里主要对方式3和方式4进行写入性能测试,单机的mac os操作系统。
mac系统,python request包实现,10000条数据,单条插入,37.1秒(短连接)
mac系统,python request包实现,10000条数据,批量插入(50条为一批),0.87秒(短连接)
mac系统,python request包实现,10000条数据,批量插入(60条为一批),0.84秒(短连接)
mac系统,python request包实现,100000条数据,批量插入(50条为一批),5.81秒(长连接)
mac系统,python request包实现,100000条数据,批量插入(50条为一批),8.33秒(短连接)