You can call the PutTimeseriesData operation to write multiple rows of time series data to a time series table at the same 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 Initialization.
Parameters
A row of time series data (timeseriesRow) includes the time series identifier (timeseriesKey) and time series data. The time series data includes data points (fields) and the time (timeInUs) of the data points. The following table describes the parameters.
Parameter | Description |
---|---|
timeseriesKey | The identifier of the time series. The identifier includes the following content:
|
timeInUs | The time of the data point. Unit: microseconds. |
fields | The data points, which can be multiple pairs of names (FieldKey) and data values (FieldValue). |
Example
/**
* Use PutTimeseriesDataSample 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 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)
}
}