You can call the GetTimeseriesData operation to query the time series data that meets the specified conditions in a time series table.
Prerequisites
Time series data is written to the time series table. For more information, see Write time series data.
A TimeseriesClient instance is initialized. For more information, see Initialize an OTSClient instance.
Parameters
Parameter | Required | Description |
timeseriesKey | Yes | The identifiers of the time series that you want to query. You can use the following parameters to specify the identifiers:
Important If you are not sure about information of the time series that you want to query, such as the metric name and data source, you can call the QueryTimeseriesMeta operation to retrieve time series based on various conditions. For more information, see Retrieve time series. |
timeRange | Yes | The time range for the query. The time range is a left-closed, right-open interval. You can use the following parameters to specify a time range:
|
backward | No | Specifies whether to sort the query results in reverse chronological order. This allows you to obtain the latest data in a time series. Valid values:
|
fieldsToGet | No | The name of the data column that you want to return. If you do not specify this parameter, all columns are returned. Important When you specify the fieldsToGet parameter, you must specify the name and data type of each column that you want to return. If the data type of a column does not match the name of the column, the column is not returned. |
limit | No | The maximum number of rows that you want to return. Note The limit parameter specifies the maximum number of rows that can be returned for a call. If the number of rows that meet the query conditions exceeds the value of the limit parameter or the number of returned rows is less than the value of the limit parameter due to the limit on the scanned data volume, you can use the nextToken parameter to return the remaining data that meets the query conditions. |
nextToken | No | The token that is used to obtain more results. If only some rows that meet the specified conditions are returned in a query, the response contains the nextToken parameter. You can specify the nextToken parameter in the next request to obtain the remaining rows. |
Examples
The following sample code provides an example on how to query the data in a time series based on the timeseriesKey:
func GetTimeseriesDataSample(client *tablestore.TimeseriesClient , timeseriesTableName string) {
fmt.Println("[Info]: Begin to get timeseries data !")
// Construct the timeseriesKey for the time series in which you want to query the data.
timeseriesKey := tablestore.NewTimeseriesKey()
timeseriesKey.SetMeasurementName("NETWORK")
timeseriesKey.SetDataSource("127.0.0.1")
timeseriesKey.AddTag("City" , "Hangzhou")
timeseriesKey.AddTag("Region" , "Xihu")
// Construct the query request.
getTimeseriesDataRequest := tablestore.NewGetTimeseriesDataRequest(timeseriesTableName)
getTimeseriesDataRequest.SetTimeseriesKey(timeseriesKey)
getTimeseriesDataRequest.SetTimeRange(0 , time.Now().UnixNano() / 1000) // Specify the time range for the query.
getTimeseriesDataRequest.SetLimit(-1)
// Call the time series client-related API operation to query data in the time series.
getTimeseriesResp , err := client.GetTimeseriesData(getTimeseriesDataRequest)
if err != nil {
fmt.Println("[Error]: Get timeseries data Failed with error: " , err)
return
}
fmt.Println("[Info]: Get timeseries data succeed ! TimeseriesRows: ")
for i := 0; i < len(getTimeseriesResp.GetRows()); i++ {
fmt.Println("[Info]: Row" , i , ": [" , getTimeseriesResp.GetRows()[i].GetTimeseriesKey().GetMeasurementName(),
getTimeseriesResp.GetRows()[i].GetTimeseriesKey().GetDataSource(),
getTimeseriesResp.GetRows()[i].GetTimeseriesKey().GetTags(), "]",
getTimeseriesResp.GetRows()[i].GetTimeInus())
rows := getTimeseriesResp.GetRows()[i].GetFieldsMap()
for key, value := range rows {
fmt.Println(key, value.Value)
}
}
fmt.Println("[Info]: GetTimeseriesDataSample finished! RequestId: " , getTimeseriesResp.RequestId)
}
FAQ
References
For more information about the API operation, see GetTimeseriesData.
You can also execute SQL statements to query time series data. For more information, see Use SQL to query time series data.
To visualize time series data, you can connect Tablestore to Grafana. For more information, see Connect Tablestore to Grafana.