當使用情境中不關心整個結果集的順序時,可以使用並發匯出資料功能以更快的速度將命中的資料全部返回。

前提條件

  • 已初始化OTSClient。具體操作,請參見初始化
  • 已建立資料表並寫入資料。
  • 已在資料表上建立多元索引。具體操作,請參見建立多元索引

參數

參數 說明
TableName 資料表名稱。
IndexName 多元索引名稱。
ScanQuery Query 多元索引的查詢語句。支援精確查詢、模糊查詢、範圍查詢、地理位置查詢、巢狀查詢等,功能和Search介面一致。
Limit 掃描資料時一次能返回的資料行數。
MaxParallel 最大並發數。請求支援的最大並發數由使用者資料量決定。資料量越大,支援的並發數越多,每次任務前可以通過ComputeSplits API進行擷取。
CurrentParallelId 當前並發ID。取值範圍為[0, MaxParallel)。
Token 用於翻頁功能。ParallelScan請求結果中有下一次進行翻頁的Token,使用該Token可以接著上一次的結果繼續讀取資料。
AliveTime ParallelScan的當前任務有效時間,也是Token的有效時間。預設值為60,建議使用預設值,單位為秒。如果在有效時間內沒有發起下一次請求,則不能繼續讀取資料。持續發起請求會重新整理Token有效時間。
说明 由於服務端採用非同步方式清理到期任務,因此當前任務只保證在設定的有效時間內不會到期,但不能保證有效時間之後一定到期。
ColumnsToGet ParallelScan目前僅可以掃描多元索引中的資料,需要在建立多元索引時設定附加儲存(即store=true)。
SessionId 本次並發掃描資料任務的SessionId。建立Session可以通過ComputeSplits API來建立,同時獲得本次任務支援的最大並發數。

樣本

/// <summary>
/// ParallelScan單並發掃描資料。
/// </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);
    }
}