If you do not have requirements on the order of query results, you can use parallel scan to quickly obtain query results.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.
  • A search index is created for the data table. For more information, see Create search indexes.

Parameters

ParameterDescription
TableNameThe name of the table.
IndexNameThe name of the search index.
ScanQueryQueryThe query statement for the search index. The query statement supports features such as term query, fuzzy query, range query, geo query, and nested query. The features that are supported by the query statement are the same as the features of the Search operation.
LimitThe maximum number of rows that can be returned by each ParallelScan call.
MaxParallelThe maximum number of parallel scan tasks per request. The maximum number of parallel scan tasks per request varies based on the data volume. A larger volume of data requires more parallel scan tasks per request. You can use the ComputeSplits operation to query the maximum number of parallel scan tasks per request.
CurrentParallelIdThe ID of the parallel scan task in the request. Valid values: [0, MaxParallel).
TokenThe token that is used to paginate query results. The results of the ParallelScan request contain the token for the next page. You can use the token to retrieve the next page.
AliveTimeThe validity period of the current parallel scan task. This validity period is also the validity period of the token. Unit: seconds. Default value: 60. We recommend that you use the default value. If the next request is not initiated within the validity period, more data cannot be queried. The validity time of the token is refreshed each time you send a request.
Note The server uses the asynchronous method to process expired tasks. The current task does not expire within the validity period. However, Tablestore does not guarantee that the task expires after the validity period.
ColumnsToGetYou can use parallel scan to scan data only in search indexes. To use parallel scan for a search index, you must set store to true when you create the search index.
SessionIdThe session ID of the parallel scan task. You can call the ComputeSplits operation to create a session and query the maximum number of parallel scan tasks that are supported by the parallel scan request.

Examples

/// <summary>
/// Perform parallel scan to scan data by using a single thread. 
/// </summary>
public class ParallelScan
{
    public static void ParallelScanwithSingleThread(OTSClient otsClient)
    {
        SearchIndexSplitsOptions options = new SearchIndexSplitsOptions
        {
            IndexName = IndexName
        };

        ComputeSplitsRequest computeSplitsRequest = new ComputeSplitsRequest
        {
            TableName = TableName,
            SplitOptions = options
        };

        ComputeSplitsResponse computeSplitsResponse = otsClient.ComputeSplits(computeSplitsRequest);

        MatchAllQuery matchAllQuery = new MatchAllQuery();

        ScanQuery scanQuery = new ScanQuery();
        scanQuery.AliveTime = 60;
        scanQuery.Query = matchAllQuery;
        scanQuery.MaxParallel = computeSplitsResponse.SplitsSize;
        scanQuery.Limit = 10;

        ParallelScanRequest parallelScanRequest = new ParallelScanRequest();
        parallelScanRequest.TableName = TableName;
        parallelScanRequest.IndexName = IndexName;
        parallelScanRequest.ScanQuery = scanQuery;
        parallelScanRequest.ColumnToGet = new ColumnsToGet { ReturnAllFromIndex = true };
        parallelScanRequest.SessionId = computeSplitsResponse.SessionId;

        int total = 0;
        List<Row> result = new List<Row>();

        ParallelScanResponse parallelScanResponse = otsClient.ParallelScan(parallelScanRequest);

        while (parallelScanResponse.NextToken != null)
        {
            List<Row> rows = new List<Row>(parallelScanResponse.Rows);

            total += rows.Count;
            result.AddRange(rows);

            parallelScanRequest.ScanQuery.Token = parallelScanResponse.NextToken;

            parallelScanResponse = otsClient.ParallelScan(parallelScanRequest);
        }

        foreach (Row row in result)
        {
            Console.WriteLine(JsonConvert.SerializeObject(row));
        }
        Console.WriteLine("Total Row Count: {0}", total);
    }
}