查询KairosDB / OpenTSDB

时间:2015-08-18 02:20:56

标签: cassandra opentsdb kairosdb

我有300万条记录,条目如下:

~/Abharthan/kairosdb$ head -10 export.txt
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"1","building_type":"ElementarySchool","meter_type":"temperature","unit":"F"},"value":"34.85"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"2","building_type":"Park","meter_type":"temperature","unit":"F"},"value":"0"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"3","building_type":"Industrial","meter_type":"temperature","unit":"F"},"value":"0.07"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"4","building_type":"RecreationCenter","meter_type":"temperature","unit":"F"},"value":"0"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"5","building_type":"Park","meter_type":"temperature","unit":"F"},"value":"2.2"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"6","building_type":"CommunityCenter","meter_type":"temperature","unit":"F"},"value":"31.41"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"7","building_type":"Office","meter_type":"temperature","unit":"F"},"value":"0"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"8","building_type":"ElementarySchool","meter_type":"temperature","unit":"F"},"value":"10.88"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"9","building_type":"ElementarySchool","meter_type":"temperature","unit":"F"},"value":"42.27"}
    {"name": "meterreadings", "timestamp":"1359695700","tags": {"Building_id":"10","building_type":"ElementarySchool","meter_type":"temperature","unit":"F"},"value":"10.14"}

这些是每个建筑物的仪表读数的1年读数,每小时收集一次building_id。

我的起始数据时间戳是:1359695700,结束时间戳是:1422853200。 我想查询此DB以获取以下内容:

Query hourly average for one building(say building_id:100) for one year (output expected of 8760 points)
Query monthly sum for one building for one year (output expected of 12 points)

我写了两个查询以获得上述两个查询的结果:

查询1:

  

{" start_absolute":1359695700," end_absolute":1422853200,   "度量":[{"标记" {" Building_id":[" 100"]}"名称" :" meterreadings""聚合":[{"名称":"平均"" align_sampling":真, "取样" {"值":" 1""单元":"小时"}}]}] }

     

回应:200   {"查询":[{" SAMPLE_SIZE":70168"结果":[{"名称":" meterreadings&#34 ;," GROUP_BY":[{"名称":"类型""类型":"数"}] "标记" {" Building_id":[" 100&#34]," building_type":["及学历"] " meter_type":["温度&#34],"单元":[" F"]}"值" :[[1359695700,42.45377343113282],[1360800000,36.42662912912908],[1364400000,41.12510250000007],[1368000000,54.915547499999946],[1371600000,65.07990000000015],[1375200000,55.8904375],[1378800000,47.33335249999986],[1382400000,38.952450000000034] [1386000000,41.99267000000001],[1389600000,41.28209500000009],[1393200000,40.31645895895911],[1396800000,40.758327499999915],[1400400000,54.05608750000002],[1404000000,63.410385],[1407600000,65.38089749999993],[1411200000,45.99822500000001],[1414800000 ,39.669450137465724],[1418400000,39.039874999999945],[1422000000,41.795917721519]]}]}]}

查询2:

  

{" start_absolute":1359695700," end_absolute":1422853200,   "度量":[{"标记" {" Building_id":[" 100"]}"名称" :" meterreadings""聚合":[{"名称":"和"" align_sampling":真, "取样" {"值":" 1""单元":"个月"}}]}] }

     

回应:200   {"查询":[{" SAMPLE_SIZE":70168"结果":[{"名称":" meterreadings&#34 ;," GROUP_BY":[{"名称":"类型""类型":"数"}] "标记" {" Building_id":[" 100&#34]," building_type":["及学历"] " meter_type":["温度&#34],"单元":[" F"]}"值" :[[1359695700,3337957.570000005]]}]}]}

我没有得到我的预期,我错过了什么。

1 个答案:

答案 0 :(得分:2)

答案很简单,正如我先前指出的可能存在的问题:-) cf。 Kairosdb error metric[0](name=abcd).tag[xyz].value may not be empty

KairosDB具有毫秒精度--KairosDB中的所有时间戳都是Unix毫秒。

但你的时间戳是在Unix秒,这就是你的问题。

因此,您需要在数据采集和查询中将所有时间戳乘以1000。

例如,query2在1970年1月16日至1970年1月17日期间不到24小时内要求所有样本,因为您在一个月内汇总只得到一个结果。

E.g。用于数据采集:

{"name": "meterreadings", "timestamp":"1359695700000","tags": {"Building_id":"1","building_type":"ElementarySchool","meter_type":"temperature","unit":"F"},"value":"34.85"}
    {"name": "meterreadings", "timestamp":"1359695700000","tags": {"Building_id":"2","building_type":"Park","meter_type":"temperature","unit":"F"},"value":"0"}

...并查询:

{ "start_absolute":1359695700000, "end_absolute":1422853200000, "metrics":[{"tags":{"Building_id":["100"]},"name":"meterreadings","aggregators":[{"name":"sum","align_sampling":true,"sampling":{"value":"1","unit":"months"}}]}]}