寫入時序資料

更新時間:
Copy as MD

建立時序表後,調用PutTimeseriesData介面向時序表寫入時序資料。該介面支援批量寫入,單次調用可寫入多行資料。

注意事項

時序模型功能需要 Tablestore Python SDK 6.1.0 及以上版本。

說明

關於 Python SDK各歷史版本的詳細資料,請參見Python SDK歷史迭代版本

前提條件

已初始化 Tablestore 用戶端。詳情請參見初始化Tablestore Client

參數說明

參數

說明

timeseriesTableName(必選

時序表名稱。

timeseriesRows(必選

時序資料列表。每行資料由時間軸標識和時間軸資料兩部分組成。

  • timeseries_key(必選):時間軸標識,包含以下欄位:

    • measurement_name(必選):時間軸的指標名稱。

    • data_source(必選):資料來源標識。

    • tags(必選):時間軸的標籤資訊,由多個索引值對(key-value)組成。

  • time_in_us(必選):資料點的時間戳記,單位為微秒。

  • fields(必選):資料點欄位集合,由多個欄位鍵(FieldKey)和欄位值(FieldValue)對組成。

樣本

以下樣本向時序表寫入兩行時序資料。

# 定義標籤資訊,用於標識時間軸
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)