Use Tablestore SDK for Java to query data in a time range from a time series, sort the data by time, select fields to return, and paginate the results.
Prerequisites
Install the Tablestore SDK for Java and initialize a time series client.
Description
Call getTimeseriesData to query data points from a single time series by specifying the complete time series identifiers and a time range. The time range includes the start time and excludes the end time.
public GetTimeseriesDataResponse getTimeseriesData(GetTimeseriesDataRequest request) throws TableStoreException, ClientException
Data written by calling putTimeseriesData can be immediately queried by calling getTimeseriesData.
The following example queries data generated within the last hour from the cpu time series in example_timeseries_table and returns up to 20 rows in a request.
TimeseriesClient timeseriesClient = client.asTimeseriesClient();
Map<String, String> tags = new HashMap<>();
tags.put("region", "cn-hangzhou");
tags.put("host", "host_0");
TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "source_0", tags);
long endTimeInUs = System.currentTimeMillis() * 1000L;
long beginTimeInUs = endTimeInUs - 3600L * 1000L * 1000L;
GetTimeseriesDataRequest request =
new GetTimeseriesDataRequest("example_timeseries_table");
request.setTimeseriesKey(timeseriesKey);
request.setTimeRange(beginTimeInUs, endTimeInUs);
request.setLimit(20);
GetTimeseriesDataResponse response = timeseriesClient.getTimeseriesData(request);
for (TimeseriesRow row : response.getRows()) {
System.out.println(row.getTimeInUs());
System.out.println(row.getFields());
}
Parameters
GetTimeseriesDataRequest contains the following parameters.
|
Name |
Type |
Description |
|
timeseriesTableName (required) |
|
The time series table name. |
|
timeseriesKey (required) |
|
The complete identifiers of the time series to query. The values must be the same as those used to write the data. |
|
beginTimeInUs (required) |
|
The start time of the query range, in microseconds since 1970-01-01 00:00:00 UTC. The value must be greater than or equal to |
|
endTimeInUs (required) |
|
The end time of the query range, in microseconds. The value must be greater than |
|
limit (optional) |
|
The maximum number of rows to return in a request. Default and maximum value: |
|
nextToken (optional) |
|
The pagination token. Do not specify this parameter in the first request. If |
|
backward (optional) |
|
Specifies whether to return data in reverse chronological order. Default: |
|
fieldsToGet (optional) |
|
The names and types of data fields to return. If this parameter is not specified, all data fields are returned. A field cannot be read if the specified data type differs from the actual data type. |
Time series identifiers
timeseriesKey is of the TimeseriesKey type and contains the following parameters.
|
Name |
Type |
Description |
|
measurementName (required) |
|
The measurement name. If an empty value was used to write the data, use an empty value in the query. |
|
dataSource (optional) |
|
The data source identifier. If an empty value was used to write the data, use an empty value in the query. |
|
tags (optional) |
|
The tags as string key-value pairs. The tag set must be the same as the set used to write the data. |
Response
GetTimeseriesDataResponse contains the following operation-specific fields.
|
Field |
Type |
Description |
|
|
|
Call |
|
|
|
Call |
Time series data rows
Each element in rows[] is of the TimeseriesRow type and contains the following fields.
|
Field |
Type |
Description |
|
|
|
Call |
|
|
|
Call |
|
|
|
Call |
Examples
Paginate all results
If a request does not return all data, repeatedly pass nextToken from the response until the returned value is null.
byte[] nextToken = null;
do {
request.setNextToken(nextToken);
GetTimeseriesDataResponse response =
timeseriesClient.getTimeseriesData(request);
for (TimeseriesRow row : response.getRows()) {
System.out.println(row.getTimeInUs());
System.out.println(row.getFields());
}
nextToken = response.getNextToken();
} while (nextToken != null);