Tablestore prend en charge le filtrage côté serveur afin de ne renvoyer que les données correspondant aux conditions spécifiées. Cette rubrique explique comment utiliser les filtres avec le SDK Node.js.
Prérequis
Types de filtres
Tablestore propose deux types de filtres :
SingleColumnCondition : filtre les données selon la valeur d'une seule colonne d'attribut.
CompositeCondition : combine plusieurs conditions de filtrage.
SingleColumnCondition
TableStore.SingleColumnCondition
Exemple
L'exemple suivant effectue une requête de plage pour les valeurs de clé primaire comprises dans l'intervalle [row1, row3). Il applique ensuite un filtre pour ne renvoyer que les lignes dont la valeur de la colonne d'attribut col1 est 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);
});
});
-
Pour exclure les lignes qui ne contiennent pas la colonne d'attribut spécifiée, définissez passIfMissing sur false.
singleColumnCondition.passIfMissing = false; -
Pour évaluer toutes les versions des données, définissez latestVersionOnly sur false. Si l'une des versions satisfait à la condition, la ligne est renvoyée.
singleColumnCondition.latestVersionOnly = false
CompositeCondition
Vous pouvez combiner jusqu'à 32 conditions.
TableStore.CompositeCondition
Exemple
L'exemple suivant effectue une requête de plage pour les lignes dont les valeurs de clé primaire se situent dans l'intervalle [row1, row3), puis applique un filtre composite.
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);
});
});