All Products
Search
Document Center

Tablestore:Write time series data

Last Updated:Jun 07, 2024

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

A row of time series data (timeseriesRow) consists of time series identifiers (timeseriesKey) and data in the time series. Data in the time series consists of the time when the data points are generated (timeInUs) and the data points (fields). The following table describes the parameters.

Parameter

Required

Description

timeseriesKey

Yes

The identifiers of the time series. You can use the following parameters to specify the identifiers:

  • measurementName: the metric name of the time series.

  • dataSource: the data source of the time series. You can leave this parameter empty.

  • tags: the tags of the time series. The value of this parameter is key-value pairs of the String type.

timeInUs

Yes

The time when the data points are generated. Unit: microseconds.

fields

Yes

The data points. Each data point consists of the name (FieldKey) and the data value (FieldValue)

Examples

The following sample code provides an example on how to write one or more rows of time series data to a time series table:

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

    // Construct timeseriesRow. 
    timeseriesKey := tablestore.NewTimeseriesKey()
    timeseriesKey.SetMeasurementName("CPU")
    timeseriesKey.SetDataSource("127.0.0.1")
    timeseriesKey.AddTag("City" , "Hangzhou")
    timeseriesKey.AddTag("Region" , "Xihu")

    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 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 the request to write the time series data. 
    putTimeseriesDataRequest := tablestore.NewPutTimeseriesDataRequest(timeseriesTableName)
    putTimeseriesDataRequest.AddTimeseriesRows(timeseriesRow , timeseriesRow1)

    // Call the time series client-related API operation to write the time series data. 
    putTimeseriesDataResponse , err := client.PutTimeseriesData(putTimeseriesDataRequest)
    if err != nil {
        fmt.Println("[Error]: Put timeseries data Failed with error: " , err)
        return
    }
    if len(putTimeseriesDataResponse.GetFailedRowResults()) > 0 {
        fmt.Println("[Warning]: Put timeseries data finished ! Some of timeseries row put Failed: ")
        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