Write time series data

更新时间:
复制 MD 格式

After you create a time series table, call the PutTimeseriesData operation to write one or more rows of time series data to the table.

Usage notes

The TimeSeries model requires Tablestore SDK for Python V6.1.0 or later.

Note

For more information, see Python SDK version history.

Prerequisites

Before you begin, ensure that you have:

Parameters

Parameter

Description

timeseriesTableName (required)

The name of the time series table.

timeseriesRows (required)

The list of time series rows to write. Each row consists of a time series identifier and the associated data.

  • timeseries_key (required): the time series identifier, which consists of the following fields:

    • measurement_name (required): The metric name of the time series.

    • data_source (required): The data source identifier.

    • tags (required): The tags for the time series, specified as key-value pairs.

  • time_in_us (required): The timestamp of the data point, in microseconds.

  • fields (required): The data points, specified as field key-value pairs.

Examples

The following example writes two rows of time series data to a time series table:

# Define tags to identify the time series
tags = {"tag1": "t1", "tag2": "t2"}

# Create time series identifiers
key1 = TimeseriesKey("measure1", "datasource1", tags)
key2 = TimeseriesKey("measure2", "datasource2", tags)

# Define data points for each time series
field1 = {"long_field": 1, "string_field": "string", "bool_field": True, "double_field": 0.3}
field2 = {"binary_field2": bytearray(b'a')}

try:
    # Build time series rows with a microsecond timestamp
    row1 = TimeseriesRow(key1, field1, int(time.time() * 1000000))
    row2 = TimeseriesRow(key2, field2, int(time.time() * 1000000))
    rows = [row1, row2]

    # Write the rows to the time series table
    ots_client.put_timeseries_data("", rows)
    print("put timeseries data succeeded.")
except Exception as e:
    print("put timeseries data failed. %s" % e)