Baidu AI Cloud
中国站

百度智能云

Time-Spatial Database

Write Operation

Write Single-field Data Points

User can refer to the following code to write single-field data points:

Note: When the metric, field, tags and timestamp written are all the same, the value written later overrides the value written first.

#Build the data points object array to write data points
datapoints = [{
    "metric": "wind",
    "tags": {
        "city": "ShangHai"
    },
    "field": "direction",  #domain name, and if this field is missing, the default is value
    "timestamp": 1531985379000,
    "type": "Long",
    "value": 1
}]
try:
    print tsdb_client.write_datapoints(datapoints)  #write data points
except BaseException as e:
    print e  # get detailed error information by catching exceptions

For the same field, if the value of one data type is written, the same field is not allowed to write other data types.

Return results:

{metadata:{keep_alive:'timeout=10',server:'BWS',connection:'keep-alive',pragma:'no-cache',date:'Wed, 01 Aug 2018 12:41:28 GMT',bce_request_id:'d46b3f74-76c4-4e1a-be47-39354b7b81a3',cache_control:'no-cache'}}

Write Multiple-Field Data Points

Multi-field data points refer to fields with multiple different names under the same metric, and data types of different fields can be different.

Different fields do not need to be written at the same time. It can be considered that different fields are independent. However, if you want to find out with one statement during the query, you need to ensure that metric, all tags and timestamps are consistent.

#Build the data points object array to write data points
datapoints = [{
    "metric": "wind",
    "tags": {
        "city": "ShangHai"
    },
    "field": "direction",
    "timestamp": 1531985380000,
    "type": "Long",
    "value": 1
}, {
    "metric": "wind",
    "tags": {
        "city": "ShangHai"
    },
    "field": "speed",
    "timestamp": 1531985380000,
    "type": "Double",
    "value": 4.5
}]
try:
    tsdb_client.write_datapoints(datapoints)  #write data points
    print 'single filed datapoint write success'
except BaseException as e:
    print e  # get detailed error information by catching exceptions
Previous
Client Create a TsdbClient
Next
Query Operation