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.
  • The TimeseriesClient is initialized. For more information, see Initialization.

Parameters

ParameterDescription
timeseriesKeyThe identifier of the time series that you want to query. The identifier includes the following content:
  • measurementName: the measurement name of the time series.
  • dataSource: the data source of the time series. You can leave this parameter empty.
  • tags: the tags of the time series. The tags are multiple key-value pairs of the STRING type.
timeRangeThe time range for the query. The time range is a left-open, right-closed interval. The time range includes the following content:
  • beginTimeInUs: the start time.
  • endTimeInUs: the end time.
backwardSpecifies whether to sort the query results in reverse chronological order. This allows you to obtain the latest data in a time series. Valid values:
  • true: sorts the query results in reverse chronological order.
  • false: sorts the query results in chronological order. This is the default value.
fieldsToGetThe columns 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 specified data type of a column is not that of the column in the time series table, the data of the column cannot be returned.
limitThe maximum number of rows that you want to return.
Note The limit parameter limits only the maximum number of rows that you want to return. Even if the number of rows that meet the specified conditions exceeds the limit, the number of rows that are returned may be less than the value of the limit parameter due to other limits such as the maximum amount of data for a scan. In this case, you can obtain the remaining rows by using the nextToken parameter.
nextTokenThe 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.

Example

/**
 * Use GetTimeseriesDataSample to query the time series data in a specific time series in a time series table based on 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 time series 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 to query the 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().GetTagsSlice(), "]",
            getTimeseriesResp.GetRows()[i].GetFieldsSlice(),
            getTimeseriesResp.GetRows()[i].GetTimeInus())
    }
    fmt.Println("[Info]: GetTimeseriesDataSample finished! RequestId: " , getTimeseriesResp.RequestId)
}