################################################################ 創建metric: 兩種方式,選擇其一即可。不管何種導入方式都必須先設置metric。 1. 事先在opentsdb中創建metric。如生成兩個名為mymetric.data_1和mymetric.data_2的metric。如下: tsdb mkmetric mymetric.data_1 mymetric.data_21 2.設置自動生成metric。修改opentsdb.conf設置: tsd.core.auto_create_metrics = true ################################################################ 數據寫入: 1.telnet方式 telnet localhost 4242 put sys.cpu.user 1436333416 23 host=web01 user=10001 2.API方式 URL: http://116.196.114.194:4242/api/put?summary 參數: [ { "metric": "sys.cpu.nice", "timestamp": 1502190111, "value": 18, "tags": { "host": "web01", "dc": "lga" } }, { "metric": "sys.cpu.nice", "timestamp": 1502190171, "value": 26, "tags": { "host": "web02", "dc": "lga" } } ] 返回: { "success": 2, "failed": 0 } 3.import方式,批量導入 導入文件格式: [root@test0926ryc001-master2 test]# cat opentsdb.txt mymetric.test.data 1479303678 0.841470984808 host=xyd_host mymetric.test.data 1479303679 0.909297426826 host=xyd_host mymetric.test.data 1479303680 0.14112000806 host=xyd_host mymetric.test.data 1479303681 -0.756802495308 host=xyd_host mymetric.test.data 1479303682 -0.958924274663 host=xyd_host 執行命令: /root/src/opentsdb-2.4.0RC2/build/tsdb import --config=/root/src/opentsdb-2.4.0RC2/opentsdb.conf opentsdb.txt ################################################################ 數據查詢: 一、返回metric=test的最近10秒的所有時序數據 1.Get方式: /api/query?start=10s-ago&m=test 2.Post方式: { "start": "10s-ago", "queries": [{ "aggregator": "none", "metric": "test" }] } 返回: [ { "metric": "test", "tags": { "device": "D47899", "label": "6015", }, "aggregateTags": [], "dps": { "1525343027": 26.26, "1525343032": 25.32 } }, { "metric": "test", "tags": { "device": "D47899", "label": "6019", }, "aggregateTags": [], "dps": { "1525343027": 25.32, "1525343032": 26.74 } }, …… { "metric": "test", "tags": { "device": "D47899", "label": "6010", }, "aggregateTags": [], "dps": { "1525343027": 26.8, "1525343032": 25.75 } } ] 二、使用filters實現tags條件查詢: 1.Get方式: /api/query?start=10s-ago&m=sum:test{device=*,label=1001|1002} 注:device和label都為tag 2.Post方式: { "start": "10s-ago", "queries": [ { "aggregator": "sum", "metric": "test", "filters": [ { "type":"wildcard", "tagk":"device", "filter":"*", "groupBy":true }, { "type":"literal_or", "tagk":"label", "filter":"1001|1002", "groupBy":true } ] } ] } 返回: [ { "metric": "test", "tags": { "label": "1001", "device": "A11223", "status": "0" }, "aggregateTags": [], "dps": { "1525344862": 24.76, "1525344867": 24.98 } }, { "metric": "app.services.temperature", "tags": { "label": "1002", "device": "A11224", "status": "0" }, "aggregateTags": [], "dps": { "1525344862": 25.75, "1525344867": 24.74 } } ] 三、對每個時序最近兩小時的數據點按小時分組計算(使用downsample) 1.Get方式: /api/query?start=2h-ago&m=sum:1h-count:test{device=*,label=1001|1002} 2.Post方式: { "start": "2h-ago", "queries": [ { "aggregator": "sum", "metric": "test", "downsample": "1h-count", "filters": [ { "type":"literal_or", "tagk":"device", "filter":"*", "groupBy":true }, { "type":"literal_or", "tagk":"label", "filter":"1001|1002", "groupBy":true } ] } ] } 返回: [ { "metric": "test", "tags": { "label": "1001", "device": "A11223", "status": "0" }, "aggregateTags": [], "dps": { "1525341600": 720, "1525345200": 91 } }, { "metric": "test", "tags": { "label": "1002", "device": "A11224", "status": "0" }, "aggregateTags": [], "dps": { "1525341600": 720, "1525345200": 91 } } ] 參考文檔: http://opentsdb.net/docs/build/html/user_guide/index.html https://blog.csdn.net/xsdxs/article/details/53882504 http://blog.51cto.com/1196740/2164968