Tablestore はサーバー側でのフィルタリングをサポートしており、指定した条件に一致するデータのみを返します。本トピックでは、Node.js SDK でフィルターを使用する方法について説明します。
前提条件
フィルターの種類
Tablestore には以下の 2 種類のフィルターがあります。
SingleColumnCondition:単一の属性列の値に基づいてデータをフィルタリングします。
CompositeCondition:複数のフィルター条件を組み合わせます。
SingleColumnCondition
TableStore.SingleColumnCondition例
以下の例では、プライマリキーの値が [row1, row3) の範囲内にあるデータに対して範囲クエリを実行し、さらにフィルターを適用して、属性列 col1 の値が val1 である行のみを返します。
var params = {
tableName: 'test_table',
// クエリの開始プライマリキーを設定します。
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// クエリの終了プライマリキーを設定します(排他的)。
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// 条件 col1 == "val1" を持つフィルターを構築します。
var singleColumnCondition = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
params.columnFilter = singleColumnCondition;
// getRange メソッドを呼び出してデータをクエリします。
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get 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);
});
});指定された属性列が存在しない行を除外するには、passIfMissing を false に設定します。
singleColumnCondition.passIfMissing = false;すべてのデータバージョンを評価するには、latestVersionOnly を false に設定します。いずれかのバージョンが条件を満たす場合、その行が返されます。
singleColumnCondition.latestVersionOnly = false
CompositeCondition
最大 32 個の条件を組み合わせることができます。
TableStore.CompositeCondition例
以下の例では、プライマリキーの値が [row1, row3) の範囲内にある行に対して範囲クエリを実行し、その後、複合フィルターを適用します。
var params = {
tableName: 'test_table',
// 開始プライマリキーを設定します。
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// 終了プライマリキーを設定します(排他的)。
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// 最初の単一列条件を構築します:col1 == "val1"。
var singleColumnCondition1 = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
// 2 番目の単一列条件を構築します:col2 == "val2"。
var singleColumnCondition2 = new TableStore.SingleColumnCondition('col2', 'val2', TableStore.ComparatorType.EQUAL);
// 最初の複合条件を構築します:col1 == "val1" OR col2 == "val2"。
var compositeCondition1 = new TableStore.CompositeCondition(TableStore.LogicalOperator.OR);
compositeCondition1.addSubCondition(singleColumnCondition1);
compositeCondition1.addSubCondition(singleColumnCondition2);
// 3 番目の単一列条件を構築します:col3 == "val3"。
var singleColumnCondition3 = new TableStore.SingleColumnCondition('col3', 'val3', TableStore.ComparatorType.EQUAL);
// 2 番目の複合条件を構築します:(col1 == "val1" OR col2 == "val2") AND col3 == "val3"。
var compositeCondition2 = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
compositeCondition2.addSubCondition(compositeCondition1);
compositeCondition2.addSubCondition(singleColumnCondition3);
// クエリにフィルターを追加します。
params.columnFilter = compositeCondition2
// getRange メソッドを呼び出してデータをクエリします。
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get 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);
});
});