Utilize o método getRange no SDK do Tablestore para Node.js a fim de ler dados dentro de um intervalo de chave primária especificado.
Pré-requisitos
Método
getRange: function getRange(params, callback)
Exemplos
O exemplo a seguir lê dados da tabela test_table onde o valor da chave primária é maior que 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);
});
});
Uma única varredura de intervalo retorna no máximo 5.000 linhas ou 4 MB de dados. Caso esse limite seja excedido, a resposta incluirá a chave primária inicial para a próxima leitura. Utilize o código a seguir para realizar consultas iterativas.
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();
Também é possível aplicar as configurações abaixo durante a consulta de dados.
-
Defina a direção de leitura dos dados.
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 }; -
Estabeleça um intervalo de versão. Somente dados dentro do intervalo definido serão retornados.
// Set the version range for the query to the last 24 hours. params.timeRange = { startTime: (Date.now() - 86400 * 1000).toString(), endTime: Date.now().toString() } -
Especifique as colunas de atributo a serem lidas.
params.columnsToGet = ['col2'] -
Configure o número máximo de linhas a retornar em uma única solicitação.
params.limit = 10