Scan time series data in parallel
Use Tablestore SDK for Java to split a time series data scan into multiple tasks and run the tasks in parallel to quickly export large amounts of data from a time series table.
Prerequisites
Install the Tablestore SDK for Java and initialize a time series client.
Description
To scan time series data in parallel, call splitTimeseriesScanTask to split the scan range into independent tasks. Then, call scanTimeseriesData for each task and run the tasks in parallel. splitCountHint specifies only the expected number of tasks. The actual number is determined by the server.
public SplitTimeseriesScanTaskResponse splitTimeseriesScanTask(SplitTimeseriesScanTaskRequest request) throws TableStoreException, ClientException
public ScanTimeseriesDataResponse scanTimeseriesData(ScanTimeseriesDataRequest request) throws TableStoreException, ClientException
The following example splits the data whose measurement name is cpu in example_timeseries_table into an expected number of four tasks. The example scans the tasks by using a parallel stream and uses nextToken to retrieve all data from each task.
TimeseriesClient timeseriesClient = client.asTimeseriesClient();
String tableName = "example_timeseries_table";
SplitTimeseriesScanTaskRequest splitRequest =
new SplitTimeseriesScanTaskRequest(tableName, "cpu", 4);
SplitTimeseriesScanTaskResponse splitResponse =
timeseriesClient.splitTimeseriesScanTask(splitRequest);
List<TimeseriesRow> rows = splitResponse.getSplitInfos().parallelStream()
.flatMap(splitInfo -> {
ScanTimeseriesDataRequest scanRequest =
new ScanTimeseriesDataRequest(tableName);
scanRequest.setSplitInfo(splitInfo);
scanRequest.setLimit(5000);
List<TimeseriesRow> splitRows = new ArrayList<>();
do {
ScanTimeseriesDataResponse scanResponse =
timeseriesClient.scanTimeseriesData(scanRequest);
splitRows.addAll(scanResponse.getRows());
scanRequest.setNextToken(scanResponse.getNextToken());
} while (scanRequest.getNextToken() != null);
return splitRows.stream();
})
.collect(Collectors.toList());
System.out.println(rows.size());
A scan may return an empty page with a non-null nextToken. Always check whether nextToken is null to determine whether the current task is complete.
Parameters
Split a scan
SplitTimeseriesScanTaskRequest contains the following parameters.
|
Name |
Type |
Description |
|
timeseriesTableName (required) |
|
The time series table name. |
|
splitCountHint (required) |
|
The expected number of tasks. The value must be greater than |
|
measurementName (optional) |
|
The measurement name to scan. |
Scan a task
ScanTimeseriesDataRequest contains the following parameters.
|
Name |
Type |
Description |
|
timeseriesTableName (required) |
|
The time series table name. This value must be the same as the value used to split the scan. |
|
splitInfo (required) |
|
The task information obtained by calling |
|
beginTimeInUs (optional) |
|
The start time of the scan range, in microseconds since 1970-01-01 00:00:00 UTC. The value must be greater than or equal to |
|
endTimeInUs (optional) |
|
The end time of the scan range, in microseconds. The value must be greater than |
|
fieldsToGet (optional) |
|
The names and types of data fields to return. If this parameter is not specified, all data fields are returned. |
|
limit (optional) |
|
The maximum number of rows to return in a request. Default and maximum value: |
|
nextToken (optional) |
|
The pagination token for the current task. Do not specify this parameter when you first scan the task. If |
Response
Split result
SplitTimeseriesScanTaskResponse contains the following operation-specific field.
|
Field |
Type |
Description |
|
|
|
Call |
Scan result
ScanTimeseriesDataResponse 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 |