寫入時序資料
更新時間:
Copy as MD
建立時序表後,調用PutTimeseriesData介面向時序表寫入時序資料。該介面支援批量寫入,單次調用可寫入多行資料。
注意事項
時序模型功能需要 Tablestore Python SDK 6.1.0 及以上版本。
說明
關於 Python SDK各歷史版本的詳細資料,請參見Python SDK歷史迭代版本。
前提條件
已初始化 Tablestore 用戶端。詳情請參見初始化Tablestore Client。
參數說明
|
參數 |
說明 |
|
timeseriesTableName(必選) |
時序表名稱。 |
|
timeseriesRows(必選) |
時序資料列表。每行資料由時間軸標識和時間軸資料兩部分組成。
|
樣本
以下樣本向時序表寫入兩行時序資料。
# 定義標籤資訊,用於標識時間軸
tags = {"tag1": "t1", "tag2": "t2"}
# 建立時間軸標識
key1 = TimeseriesKey("measure1", "datasource1", tags)
key2 = TimeseriesKey("measure2", "datasource2", tags)
# 定義各時間軸的資料點欄位
field1 = {"long_field": 1, "string_field": "string", "bool_field": True, "double_field": 0.3}
field2 = {"binary_field2": bytearray(b'a')}
try:
# 構建時序資料行,時間戳記單位為微秒
row1 = TimeseriesRow(key1, field1, int(time.time() * 1000000))
row2 = TimeseriesRow(key2, field2, int(time.time() * 1000000))
rows = [row1, row2]
# 調用介面寫入時序資料
ots_client.put_timeseries_data("<TIMESERIES_TABLE_NAME>", rows)
print("put timeseries data succeeded.")
except Exception as e:
print("put timeseries data failed. %s" % e)
该文章对您有帮助吗?