當使用場景中不關心整個結果集的順序時,您可以使用并發導出數據功能以更快的速度將命中的數據全部返回。
前提條件
已初始化OTSClient。具體操作,請參見初始化OTSClient。
已在數據表上創建多元索引。具體操作,請參見創建多元索引。
參數
參數 | 說明 | |
TableName | 數據表名稱。 | |
IndexName | 多元索引名稱。 | |
ScanQuery | Query | 多元索引的查詢語句。支持精確查詢、模糊查詢、范圍查詢、地理位置查詢、嵌套查詢等,功能和Search接口一致。 |
Limit | 掃描數據時一次能返回的數據行數。 | |
MaxParallel | 最大并發數。請求支持的最大并發數由用戶數據量決定。數據量越大,支持的并發數越多,每次任務前可以通過ComputeSplits API進行獲取。 | |
CurrentParallelId | 當前并發ID。取值范圍為[0, MaxParallel)。 | |
Token | 用于翻頁功能。ParallelScan請求結果中有下一次進行翻頁的Token,使用該Token可以接著上一次的結果繼續讀取數據。 | |
AliveTime | ParallelScan的當前任務有效時間,也是Token的有效時間。默認值為60,建議使用默認值,單位為秒。如果在有效時間內沒有發起下一次請求,則不能繼續讀取數據。持續發起請求會刷新Token有效時間。 說明 動態修改schema中的切換索引、服務端單臺機器故障、服務端負載均衡等均會導致Session提前過期,此時需要重新創建Session。 | |
ColumnsToGet | 指定分組結果中需要返回的列名,可以通過將列名加入Columns來實現。 如果需要返回多元索引中的所有列,則可以使用更簡潔的ReturnAllFromIndex實現。 重要 此處不能使用ReturnAll。 | |
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);
}
}