Query time series data
Query time series data that meets specific conditions from a time series table using the Tablestore SDK for Java.
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 a Tablestore client.
Parameters
|
Parameter |
Required |
Description |
|
timeseriesKey |
Yes |
The identifiers of the time series to query. Use the following sub-parameters to specify identifiers:
Important
If you are unsure of the time series identifiers, such as the metric name or data source, call the QueryTimeseriesMeta operation to retrieve time series based on filter conditions. For more information, see Retrieve time series. |
|
timeRange |
Yes |
The time range for the query. The range is a left-closed, right-open interval. Use the following sub-parameters:
|
|
backward |
No |
Specifies whether to return results in reverse chronological order. Use this to retrieve the latest data points first. Valid values:
|
|
fieldsToGet |
No |
The data columns to return. If not specified, all columns are returned. Important
When specifying fieldsToGet, you must provide both the name and data type of each column. If the data type does not match the column name, the column is not returned. |
|
limit |
No |
The maximum number of rows to return per call. Note
If the result set exceeds the limit, or if fewer rows are returned due to scanned data volume constraints, use the nextToken parameter to retrieve the remaining rows. |
|
nextToken |
No |
The pagination token for retrieving additional results. If a query returns only a partial result set, the response includes nextToken. Pass this token in the next request to get the remaining rows. Important
To persist or transfer nextToken to a frontend page, encode it using Base64. Do not use |
Examples
The following example queries data in a time series by specifying 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 to Grafana.
-
To store time series data at low costs and quickly query and analyze time series data, you can use time series analytics stores. For more information, see Time series analytics storage.