Write time series data
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
-
A time series table is created. For more information, see Create a time series table.
-
A TimeseriesClient instance is initialized. For more information, see Initialize an OTSClient instance.
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:
|
|
timeInUs |
Yes |
The timestamp of the data point, in microseconds. |
|
fields |
Yes |
The data points, as one or more |
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
For more information about the API operation, see PutTimeseriesData.
After you write data, query it from the time series table. For more information, see Query time series data.
To retrieve time series that meet specific conditions, see Retrieve time series.
To update or delete time series metadata, see Update a time series and Delete a time series.
To migrate time series data from one time series table to another, use the DataWorks data integration service. For more information, see Synchronize data from one time series table to another.
To migrate data from a Kafka data source to a Tablestore time series table, use the Tablestore Sink Connector. For more information, see Synchronize Kafka data to a time series table.