Tablestore admite el filtrado en el servidor para devolver únicamente los datos que cumplen con las condiciones especificadas. Este tema explica cómo utilizar filtros con el SDK de Node.js.
Requisitos previos
Tipos de filtro
Tablestore proporciona dos tipos de filtros:
SingleColumnCondition: filtra los datos según el valor de una sola columna de atributo.
CompositeCondition: combina varias condiciones de filtro.
SingleColumnCondition
TableStore.SingleColumnCondition
Ejemplo
El siguiente ejemplo realiza una consulta por rango para valores de clave primaria dentro del intervalo [row1, row3). A continuación, aplica un filtro para devolver solo las filas donde el valor de la columna de atributo col1 es 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 las filas que no contienen la columna de atributo especificada, establezca passIfMissing en false.
singleColumnCondition.passIfMissing = false; -
Para evaluar todas las versiones de datos, establezca latestVersionOnly en false. Si alguna versión de los datos cumple la condición, se devuelve la fila.
singleColumnCondition.latestVersionOnly = false
CompositeCondition
Es posible combinar hasta 32 condiciones.
TableStore.CompositeCondition
Ejemplo
El siguiente ejemplo realiza una consulta por rango para filas con valores de clave primaria en el intervalo [row1, row3) y luego aplica un filtro compuesto.
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);
});
});