Baca data dalam rentang kunci primer dari tabel Tablestore menggunakan metode getRange pada Node.js SDK.
Prasyarat
Metode
getRange: function getRange(params, callback)
Contoh
Kode contoh berikut menunjukkan cara membaca data dari tabel test_table dengan nilai kunci primer lebih besar dari row1.
var params = {
tableName: 'test_table',
// Set the start primary key for the query.
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// Set the end primary key for the query. The end primary key is not included in the result.
exclusiveEndPrimaryKey: [{ 'id': TableStore.INF_MAX }]
};
// Call the getRange method to query data.
client.getRange(params, function (err, data) {
if (err) {
console.log('Get range failed with error: ', err);
return;
}
console.log('* RequestId: ', data.RequestId);
console.log('* Read CU Cost: ', data.consumed.capacityUnit.read);
console.log('* Write CU Cost: ', data.consumed.capacityUnit.write);
console.log('* Rows Data: ');
data.rows.forEach(function (row) {
console.log(row);
});
});
Satu kali pemindaian rentang dapat mengembalikan maksimal 5.000 baris atau data sebesar 4 MB. Jika batas ini terlampaui, operasi akan mengembalikan kunci primer awal untuk pembacaan berikutnya. Anda dapat menggunakan kode berikut untuk melakukan permintaan iteratif.
async function getRangeSample() {
try {
while (true) {
// Call the getRange method to query data.
const data = await client.getRange(params);
// Process the returned result.
console.log('* RequestId: ', data.RequestId);
console.log('* Read CU Cost: ', data.consumed.capacityUnit.read);
console.log('* Write CU Cost: ', data.consumed.capacityUnit.write);
console.log('* Rows Data: ');
data.rows.forEach(function (row) {
console.log(row);
});
// Set the start primary key for the next read.
if (data.nextStartPrimaryKey) {
params.inclusiveStartPrimaryKey = data.nextStartPrimaryKey.map(item => {
return {
[item.name]: item.value
};
});
} else {
break;
}
}
} catch (err) {
console.log('Range get failed with error: ', err);
}
}
getRangeSample();
Konfigurasikan juga pengaturan berikut saat melakukan kueri data.
-
Tetapkan arah pembacaan data.
var params = { tableName: 'test_table', // Set the start primary key for the query. When you read data in reverse order, the start primary key must be greater than the end primary key. inclusiveStartPrimaryKey: [{ 'id': TableStore.INF_MAX }], // Set the end primary key for the query. The end primary key is not included in the result. exclusiveEndPrimaryKey: [{ 'id': 'row1' }], // Set the read direction to reverse. direction: TableStore.Direction.BACKWARD }; -
Tetapkan rentang versi data yang akan dibaca. Operasi hanya akan mengembalikan data dalam rentang versi yang ditentukan.
// Set the version range for the query to the last 24 hours. params.timeRange = { startTime: (Date.now() - 86400 * 1000).toString(), endTime: Date.now().toString() } -
Tentukan kolom atribut yang akan dibaca.
params.columnsToGet = ['col2'] -
Tetapkan jumlah maksimum baris yang dikembalikan dalam satu permintaan.
params.limit = 10