查询操作

获取度量(Metric)

如下代码可以获取metric列表:

# 获取metric列表
result = tsdb_client.get_metrics()
print result.metrics

返回结果:

[u'wind']

获取域(Field)

如下代码可以获取field对象:

metric = 'wind' # 要获取field所属的metric
result = tsdb_client.get_fields(metric) # 获取指定metric下的所有field
print result.fields

返回结果:

{direction:{type:u'Number'},speed:{type:u'Number'}}

获取标签(Tag)

如下代码可以获取tag对象:

metric = 'wind' # 要获取tag所属的metric
result = tsdb_client.get_tags(metric)
print result.tags

返回结果:

{city:[u'ShangHai']}

查询数据点

查询单域数据点

如下代码可以查询单域数据点:

#构建查询对象
query_list = [{
    "metric": "wind",
    "field": "direction",
    "filters": {
        "start": 1531985370000,
        "end": 1531985400000,
        "tags": {
            "city": ["ShangHai"]
        },
        "value": ">= 0"
    },
    "groupBy": [{
        "name": "Tag",
        "tags": ["city"]
    }],
    "limit": 1000,
    "aggregators": [{
        "name": "Sum",
        "sampling": "10 minutes"
    }]
}]
result = tsdb_client.get_datapoints(query_list)
print result.results

返回结果:

[{field:u'direction',metric:u'wind',groups:[{group_infos:[{name:u'Tag',tags:{city:u'ShangHai'}}],values:[[1531985379000, 2]]}],rawcount:2}]

查询多域数据点

如下代码可以查询多域数据点:

#构建查询对象
query_list = [{
    "metric": "wind",
    "fields": ["direction", "speed"],   #查询多个域
    "filters": {
        "start": 1531985370000,
        "end": 1531985400000,
        "tags": {
            "city": ["ShangHai"]
        },
        "value": ">= 0"
    },
    "groupBy": [{
        "name": "Tag",
        "tags": ["city"]
    }],
    "limit": 1000,
    "aggregators": [{
        "name": "Sum",
        "sampling": "10 minutes"
    }]
}]
result = tsdb_client.get_datapoints(query_list)
print result.results

返回结果:

[{fields:[u'direction', u'speed'],metric:u'wind',groups:[{group_infos:[{name:u'Tag',tags:{city:u'ShangHai'}}],values:[[1531985380000, 1, 4.5]]}],rawcount:1}]

使用插值方式查询数据

使用如下代码进行插值查询:

# 构建查询对象
query_list = [{
    "metric": "wind",
    "fields": ["direction", "speed"],
    "filters": {
        "start": 1531985370000,
        "end": 1532985370000,
        "tags": {
            "city": ["ShangHai"]
        },
        "value": ">= 0"
    },
    "fill": {                             #配置插值参数
        "type": "Linear",                 #插值类型
        "interval": "1 day",              #插值间隔
        "maxWriteInterval": "30 minutes"  #最大写入间隔
    },
    "groupBy": [{
        "name": "Tag",
        "tags": ["city"]
    }],
    "limit": 1000,
    "aggregators": [{
        "name": "Sum",
        "sampling": "10 minutes"
    }]
}]

result = tsdb_client.get_datapoints(query_list)
print result.results

返回结果:

[{fields:[u'direction', u'speed'],metric:u'wind',groups:[{group_infos:[{name:u'Tag',tags:{city:u'ShangHai'}}],values:[[1531985380000, 1, 4.5], [1532071780000, 1, 4.5], [1532158180000, 1, 4.5], [1532244580000, 1, 4.5], [1532330980000, 1, 4.5], [1532417380000, 1, 4.5], [1532503780000, 1, 4.5], [1532590180000, 1, 4.5], [1532676580000, 1, 4.5], [1532762980000, 1, 4.5], [1532849380000, 1, 4.5], [1532935780000, 1, 4.5]]}],rawcount:12}]

分页查询数据点

如果查询到的原始数据点过大,会分多次返回,使用marker参数来进行分页查询。

# 通过循环来多次查询数据

query_list = [{
    "metric": "wind",
    "field": "direction",
    "filters": {
        "start": 1531985300000,
        "end": 1531985400000,
        "tags": {
            "city": ["ShangHai"]
        }
    },
    "limit": 1
}]
count = 0
while True:
    count += 1
    if len(query_list) >0:
        result = tsdb_client.get_datapoints(query_list)
        print count, result.results
    else:
        print 'end query'
        break
    next_query = []
    for i in range(len(query_list)):
        if result.results[i].truncated:
            query_list[i]['marker'] = result.results[i].next_marker
            next_query.append(query_list[i])
    query_list = next_query

返回结果:

1 [{field:u'direction',next_marker:u'AAABZLFxwqBjaXR5PVNoYW5nSGFp',groups:[{group_infos:[],values:[[1531985379000, 1]]}],truncated:True,metric:u'wind',rawcount:1}]
2 [{field:u'direction',metric:u'wind',groups:[{group_infos:[],values:[[1531985380000, 1]]}],rawcount:1}]
end query

使用SQL查询数据点

TSDB支持使用SQL进行数据查询,示例代码如下:

sql = "select * from wind"
result = tsdb_client.get_rows_with_sql(sql)
print result

返回结果:

{rows:[[1531985379000, 1.0, None, u'ShangHai'], [1531985380000, 1.0, 4.5, u'ShangHai']],columns:[{name:u'timestamp'}, {name:u'direction'}, {name:u'speed'}, {name:u'city'}],metadata:{content_length:'168',content_type:'application/json; charset=UTF-8',keep_alive:'timeout=10',server:'BWS',connection:'keep-alive',pragma:'no-cache',date:'Thu, 02 Aug 2018 00:35:28 GMT',bce_request_id:'46f5d19e-58de-4654-bc35-91bb67b58e76',cache_control:'no-cache'}}