本文介紹如何通過 Node.js SDK 批量讀取Table Store中的資料,支援查詢多個表的資料。
注意事項
單次批量讀取操作最多支援讀取 100 行資料。
前提條件
方法說明
batchGetRow: function batchGetRow(params, callback)範例程式碼
以下範例程式碼用於讀取 test_table 表中主索引值為 row1 和 row2 的兩行資料。
var table = {
// 設定表名稱
tableName: 'test_table',
primaryKey: [
// 添加第 1 行主鍵資訊
[{ 'id': 'row1' }],
// 添加第 2 行主鍵資訊
[{ 'id': 'row2' }]
]
};
var params = {
tables: [
table
]
}
// 調用 batchGetRow 方法進行批量資料查詢
client.batchGetRow(params, function (err, data) {
if (err) {
console.log('Batch get row failed with error: ', err);
return;
}
// 返回結果處理
console.log('* RequestId: ', data.RequestId);
console.log('* Rows Data: ');
data.tables.forEach(function (rows) {
rows.forEach(function (row) {
if (row.isOk) {
console.log('Succeeded Row:', row.tableName, JSON.stringify(row.primaryKey), JSON.stringify(row.attributes));
} else {
console.log('Failed Row:', row.tableName, row.errorMessage);
}
});
});
});您可以在批量讀取資料時參考以下範例程式碼進行參數設定。
讀取多張表的資料。批量讀取支援一次讀取多張表的資料,您需要為每張表指定查詢條件。
var table1 = { tableName: 'orders_small', primaryKey: [ [{ 'order_id': '90fb478c-1360-11f0-a34d-00163e30a2a9' }] ] }; var params = { tables: [ table, table1 ] };設定讀取的資料版本範圍,結果只返回版本範圍內的資料。
// 設定查詢的資料版本範圍為目前時間往前一天 table.timeRange = { startTime: (Date.now() - 86400 * 1000).toString(), endTime: Date.now().toString() }指定讀取的屬性列。
table.columnsToGet = ['col2']