官方文档,http://opentsdb.net/docs/build/html/index.html
目前最新版本是2.4
1. 安装
然后发现window不是很方便安装,那就转战使用Docker安装吧
查看文档,http://opentsdb.net/docs/build/html/resources.html
看文档发现可以有两个镜像可以使用,但是实际过程中第二个opower/opentsdb
不能使用
-
petergrace/opentsdb-docker
A prebuilt Docker image with HBase and OpenTSDB already configured and ready to run! If you have Docker installed, executedocker run -d -p 4242:4242 petergrace/opentsdb-docker
to create an opentsdb instance running on port 4242. -
opower/opentsdb
A Docker image containing OpenTSDB, HBase, and tcollector. Comes in both 2.0.1 and 2.1 versions (latest defaults to 2.1). Executedocker run -d -p 4242:4242 opower/opentsdb
to create an OpenTSDB instance running on port 4242.
``
这里选用第一个petergrace/opentsdb-docker
,运行命令安装即可
然后访问页面http://localhost:4242
会出现一个OpenTSDB自带的数据查询页面
2. 如何建表
答案:目前此种安装方式不用建表
在OpenTSDB中,所有数据存储在一张叫做tsdb的表中,这是为了充分利用hbase有序和region分布式的特点。所有的值都保存在列族t中。
3. 存储数据
利用OpenTSDB提供的HTTP API接口进行数据存储。示例代码如下
"""
如何insert数据
"""
import requests
data1 = {
"metric": "fan",
"timestamp": '1613712046000',
"value": '21',
"tags": {
"imei": "123456ABC",
"tag": "rmf_abc"
}
}
data2 = {
"metric": "fan",
"timestamp": '1613712047000',
"value": '22',
"tags": {
"imei": "123456ABC",
"tag": "rmf_abc"
}
}
data3 = {
"metric": "fan",
"timestamp": '1613712048000',
"value": '23',
"tags": {
"imei": "123456ABC",
"tag": "rmf_abc"
}
}
data4 = {
"metric": "fan",
"timestamp": '1613712049000',
"value": '24',
"tags": {
"imei": "123456ABC",
"tag": "rmf_abc"
}
}
list1 = [data1, data2, data3, data4]
def send_json(json):
result = requests.post("http://192.168.3.67:4242/api/put?details", json=json)
return result.text
def main():
print(send_json(list1))
if __name__ == "__main__":
main()
4. 获取数据
获取数据查看的脚本如下,目前大多数是以秒级为单位返回数据
import requests
def data_get(query):
result = requests.get("http://192.168.3.67:4242/api/query?" + query)
jsonresponse = result.json()[0]
return jsonresponse
if __name__ == "__main__":
print(data_get('start=1613712042000&m=sum:fan{imei=123456ABC}'))
5. 基本概念
每一个时间序列时间点包含如下内容:
- metric,为该时间序列数据定义的名称,如
sys.cpu.user
,stock.quote
或者env.probe.temp
- timestamp,标准为Unix或POSIX Epoch的时间戳,可以秒或者毫秒为单位。且必须是正数
- value,数值,可为整数或者浮点数
- tag(s),每个数据点之至少有一个key-value键值对作为tag
其他可参考文章