當使用場景中不關心整個結果集的順序時,您可以使用并發導出數據功能以更快的速度將命中的數據全部返回。
前提條件
參數
參數 | 說明 | |
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來創建,同時獲得本次任務支持的最大并發數。 |
示例
以下示例用于并發導出數據。
//1.獲取sessionId。
let computeSplits = await new Promise((resolve, reject) => {
client.computeSplits({
tableName: tableName,
searchIndexSplitsOptions: {
indexName: indexName,
}
}, function (err, data) {
if (err) {
console.log('computeSplits error:', err.toString());
reject(err);
} else {
console.log('computeSplits success:', data);
resolve(data)
}
})
})
//2.構造query。
const scanQuery = {
query: {
queryType: TableStore.QueryType.MATCH_ALL_QUERY,
},
limit: 1000,
aliveTime: 30,
token: undefined,
currentParallelId: 0,
maxParallel: 1,
}
//3.構造ParallelScan請求(該示例為了方便介紹使用同步請求進行展示,實際業務中可修改為異步)。
const parallelScanPromise = function () {
return new Promise(function (resolve, reject) {
client.parallelScan({
tableName: tableName,
indexName: indexName,
columnToGet: {
returnType: TableStore.ColumnReturnType.RETURN_ALL_FROM_INDEX,
},
sessionId: computeSplits.sessionId,
scanQuery: scanQuery,
}, function (err, data) {
if (err) {
console.log('parallelScan error:', err.toString());
reject(err);
} else {
console.log("parallelScan, rows:", data.rows.length)
resolve(data)
}
});
})
}
let totalCount = 0 //示例代碼記錄總行數。
let parallelScanResponse = await parallelScanPromise()
totalCount = totalCount + parallelScanResponse.rows.length
//4.迭代拉取數據,直到拉取所有數據結束。
while (parallelScanResponse.nextToken !== null && parallelScanResponse.nextToken.length > 0) {
scanQuery.token = parallelScanResponse.nextToken
parallelScanResponse = await parallelScanPromise()
totalCount += parallelScanResponse.rows.length
}
console.log("total rows:", totalCount)
常見問題
相關文檔
多元索引查詢類型包括精確查詢、多詞精確查詢、全匹配查詢、匹配查詢、短語匹配查詢、前綴查詢、范圍查詢、通配符查詢、多條件組合查詢、地理位置查詢、嵌套類型查詢、向量檢索和列存在性查詢,您可以選擇合適的查詢類型進行多維度數據查詢。
如果要對結果集進行排序或者翻頁,您可以使用排序和翻頁功能來實現。具體操作,請參見排序和翻頁。
如果要按照某一列對結果集做折疊,使對應類型的數據在結果展示中只出現一次,您可以使用折疊(去重)功能來實現。具體操作,請參見折疊(去重)。
如果要進行數據分析,例如求最值、求和、統計行數等,您可以使用Search接口的統計聚合功能或者SQL查詢來實現。具體操作,請參見統計聚合和SQL查詢。
如果要快速導出數據,而不關心整個結果集的順序時,您可以使用ParallelScan接口和ComputeSplits接口實現多并發導出數據。具體操作,請參見并發導出數據。