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.
- The TimeseriesClient is initialized. For more information, see Initialize an OTSClient instance.
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:
|
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
For more information about the API operation, see PutTimeseriesData.
After you write time series data, you can read the time series data in the time series table based on your business requirements. For more information, see Query time series data.
You can retrieve time series that meet specific conditions. For more information, see Retrieve time series.
If the attributes in the metadata of a time series do not meet your business requirements, you can update or delete time series metadata. For more information, see Update time series metadata and Delete time series metadata.
If you want to migrate data from Apache Kafka to time series tables in Tablestore, you can use Tablestore Sink Connector. For more information, see Data synchronization to time series tables.