Write time series data

更新时间: 2026-04-29 17:48:00

After you create a time series table, you can call the PutTimeseriesData operation to write multiple rows of time series data to the time series table at a time.

Prerequisites

Parameters

Each row of time series data (timeseriesRow) consists of a time series identifier (timeseriesKey) and the data itself. The data contains the timestamp (timeInUs) and one or more field values (fields).

timeseriesKey identifies the metric type (such as CPU or NETWORK), data source host, and key-value labels. fields holds the measured values for each data point.

Parameter

Required

Description

timeseriesKey

Yes

The time series identifier. It includes the following information:

  • measurementName: The measurement name of the time series.

  • dataSource: The data source. This can be empty.

  • tags: Tags for the time series, expressed as key-value pairs of strings.

timeInUs

Yes

The timestamp of the data point, in microseconds.

fields

Yes

The data points, as one or more FieldKeyFieldValue pairs.

Example

The following example writes two rows of time series data — one for CPU metrics and one for network metrics — to a time series table using PutTimeseriesData.

timeseriesKey defines the identity that groups related data points. timeseriesRow attaches the timestamp and field values to that identity.

func PutTimeseriesDataSample(client *tablestore.TimeseriesClient , timeseriesTableName string) {
    fmt.Println("[Info]: Begin to PutTimeseriesDataSample !")

    // Construct the time series row timeseriesRow.
    // timeseriesKey identifies the time series — measurement name, source host, and tags.
    timeseriesKey := tablestore.NewTimeseriesKey()
    timeseriesKey.SetMeasurementName("CPU")
    timeseriesKey.SetDataSource("127.0.0.1")
    timeseriesKey.AddTag("City" , "Hangzhou")
    timeseriesKey.AddTag("Region" , "Xihu")

    // timeseriesRow attaches the timestamp and field values to the key.
    timeseriesRow := tablestore.NewTimeseriesRow(timeseriesKey)
    timeseriesRow.SetTimeInus(time.Now().UnixNano() / 1000)
    timeseriesRow.AddField("temperature" , tablestore.NewColumnValue(tablestore.ColumnType_INTEGER , 98))
    timeseriesRow.AddField("status" , tablestore.NewColumnValue(tablestore.ColumnType_STRING , "ok"))

    // Construct the time series row timeseriesRow1.
    timeseriesKey1 := tablestore.NewTimeseriesKey()
    timeseriesKey1.SetMeasurementName("NETWORK")
    timeseriesKey1.SetDataSource("127.0.0.1")
    timeseriesKey1.AddTag("City" , "Hangzhou")
    timeseriesKey1.AddTag("Region" , "Xihu")

    timeseriesRow1 := tablestore.NewTimeseriesRow(timeseriesKey1)
    timeseriesRow1.SetTimeInus(time.Now().UnixNano() / 1000)
    timeseriesRow1.AddField("in" , tablestore.NewColumnValue(tablestore.ColumnType_INTEGER , 1000))
    timeseriesRow1.AddField("data" , tablestore.NewColumnValue(tablestore.ColumnType_BINARY , []byte("tablestore")))
    timeseriesRow1.AddField("program" , tablestore.NewColumnValue(tablestore.ColumnType_STRING , "tablestore.d"))
    timeseriesRow1.AddField("status" , tablestore.NewColumnValue(tablestore.ColumnType_BOOLEAN, true))
    timeseriesRow1.AddField("lossrate" , tablestore.NewColumnValue(tablestore.ColumnType_DOUBLE , float64(1.9098)))

    // Construct a request to write time series data.
    putTimeseriesDataRequest := tablestore.NewPutTimeseriesDataRequest(timeseriesTableName)
    putTimeseriesDataRequest.AddTimeseriesRows(timeseriesRow , timeseriesRow1)

    // Call the time series client to write the time series data.
    putTimeseriesDataResponse , err := client.PutTimeseriesData(putTimeseriesDataRequest)
    if err != nil {
        fmt.Println("[Error]: Failed to put time series data. Error: " , err)
        return
    }

    // A batch write may partially succeed. Failed rows are returned with their index and
    // error message, while successfully written rows are committed immediately.
    if len(putTimeseriesDataResponse.GetFailedRowResults()) > 0 {
        fmt.Println("[Warning]: Put time series data finished. Some rows failed to be written: ")
        for i := 0; i < len(putTimeseriesDataResponse.GetFailedRowResults()); i++ {
            FailedRow := putTimeseriesDataResponse.GetFailedRowResults()[i]
            fmt.Println("[Warning]: Failed Row: Index: " , FailedRow.Index , " Error: " , FailedRow.Error)
        }
    } else {
        fmt.Println("[Info]: PutTimeseriesDataSample finished. RequestId: " , putTimeseriesDataResponse.RequestId)
    }
}

FAQ

References

上一篇: Operations on time series data 下一篇: Query time series data
阿里云首页 表格存储 相关技术圈