Query Operation
Last Updated:2020-07-20
Get the Metric
The following code can get the metric list:
# get metric lists
result = tsdb_client.get_metrics()
print result.metrics
Return result:
[u'wind']
Get the Field
The following code can get the Field objects:
metric = 'wind' # metric that the field to get belongs to
result = tsdb_client.get_fields(metric) # get all the fields under the appointed metric
print result.fields
Return result:
{direction:{type:u'Number'},speed:{type:u'Number'}}
Get the Tag
The following code can get the Tag objects:
metric = 'wind' # metric that the tag to get belongs to
result = tsdb_client.get_tags(metric)
print result.tags
Return result:
{city:[u'ShangHai']}
Query Data Points
Query Single-Field Data Points
The following code can query single-field data points:
# build query objects
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
Return result:
[{field:u'direction',metric:u'wind',groups:[{group_infos:[{name:u'Tag',tags:{city:u'ShangHai'}}],values:[[1531985379000, 2]]}],rawcount:2}]
Query Multiple-Field Data Points
The following code can query multiple-field data points:
# build query objects
query_list = [{
"metric": "wind",
"fields": ["direction", "speed"], # query multiple fields
"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
Return result:
[{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 Data Points Using Interpolation
Use following code for interpolation query:
# build query objects
query_list = [{
"metric": "wind",
"fields": ["direction", "speed"],
"filters": {
"start": 1531985370000,
"end": 1532985370000,
"tags": {
"city": ["ShangHai"]
},
"value": ">= 0"
},
"fill": { # configure interpolation parameters
"type": "Linear", # interpolation type
"interval": "1 day", # interpolation interval
"maxWriteInterval": "30 minutes" # max write interval
},
"groupBy": [{
"name": "Tag",
"tags": ["city"]
}],
"limit": 1000,
"aggregators": [{
"name": "Sum",
"sampling": "10 minutes"
}]
}]
result = tsdb_client.get_datapoints(query_list)
print result.results
Return result:
[{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}]
Paging Query Data Points
If the original data point queried is too large, it will be returned several times and the marker parameter will be used for paging query.
# query data multiple times through loop
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
Return result:
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
Query Data Points Using SQL
TSDB supports data queries using SQL. The example code is as follows:
sql = "select * from wind"
result = tsdb_client.get_rows_with_sql(sql)
print result
Return 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'}}