O Tablestore oferece filtragem no servidor para retornar apenas os dados correspondentes às condições especificadas. Este tópico explica como usar filtros com o SDK Node.js.
Pré-requisitos
Tipos de filtro
O Tablestore fornece dois tipos de filtros:
SingleColumnCondition: filtra dados com base no valor de uma única coluna de atributo.
CompositeCondition: combina várias condições de filtragem.
SingleColumnCondition
TableStore.SingleColumnCondition
Exemplo
O exemplo a seguir executa uma consulta por intervalo para valores de chave primária entre [row1, row3). Em seguida, aplica um filtro para retornar somente as linhas em que o valor da coluna de atributo col1 seja val1.
var params = {
tableName: 'test_table',
// Set the start primary key for the query.
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// Set the end primary key for the query (exclusive).
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// Construct a filter with the condition: col1 == "val1".
var singleColumnCondition = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
params.columnFilter = singleColumnCondition;
// Call the getRange method to query data.
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get failed with error: ', err);
return;
}
// Process the response.
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);
});
});
-
Para excluir linhas sem a coluna de atributo especificada, defina passIfMissing como false.
singleColumnCondition.passIfMissing = false; -
Para avaliar todas as versões dos dados, defina latestVersionOnly como false. A linha retorna se qualquer versão atender à condição.
singleColumnCondition.latestVersionOnly = false
CompositeCondition
Você pode combinar até 32 condições.
TableStore.CompositeCondition
Exemplo
O exemplo a seguir executa uma consulta por intervalo para linhas com valores de chave primária entre [row1, row3) e aplica um filtro composto.
var params = {
tableName: 'test_table',
// Set the start primary key.
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// Set the end primary key (exclusive).
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// Construct the first single column condition: col1 == "val1".
var singleColumnCondition1 = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
// Construct the second single column condition: col2 == "val2".
var singleColumnCondition2 = new TableStore.SingleColumnCondition('col2', 'val2', TableStore.ComparatorType.EQUAL);
// Construct the first composite condition: col1 == "val1" OR col2 == "val2".
var compositeCondition1 = new TableStore.CompositeCondition(TableStore.LogicalOperator.OR);
compositeCondition1.addSubCondition(singleColumnCondition1);
compositeCondition1.addSubCondition(singleColumnCondition2);
// Construct the third single column condition: col3 == "val3".
var singleColumnCondition3 = new TableStore.SingleColumnCondition('col3', 'val3', TableStore.ComparatorType.EQUAL);
// Construct the second composite condition: (col1 == "val1" OR col2 == "val2") AND col3 == "val3".
var compositeCondition2 = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
compositeCondition2.addSubCondition(compositeCondition1);
compositeCondition2.addSubCondition(singleColumnCondition3);
// Add the filter to the query.
params.columnFilter = compositeCondition2
// Call the getRange method to query data.
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get failed with error: ', err);
return;
}
// Process the response.
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);
});
});