Table Store支援在查詢資料時使用過濾器在服務端按指定條件進行資料過濾,本文介紹如何在 Node.js SDK 中使用過濾器。
前提條件
過濾器類型
Table Store的過濾器類型包括以下 2 種。
單屬性列值過濾器(SingleColumnCondition):判斷單個屬性列的值是否符合條件。
組合過濾器(CompositeCondition):將多個條件組合進行資料過濾。
單屬性列值過濾器
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);
});
});設定行資料不包含判斷的屬性列時,不返回該行。
singleColumnCondition.passIfMissing = false;設定判斷查詢結果中所有版本的資料,此時只要有一個版本的資料符合條件,即返回該行資料。
singleColumnCondition.latestVersionOnly = false
組合過濾器
最多支援 32 個條件的組合。
TableStore.CompositeCondition範例程式碼
以下範例程式碼以範圍查詢為例查詢主索引值為 [row1, row3) 的行資料,並使用組合過濾器進行資料過濾。
var params = {
tableName: 'test_table',
// 設定查詢起始主鍵
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// 設定查詢結束主鍵(返回結果不包含結束主鍵)
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// 構造單屬性列值過濾器1,條件為 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);
// 構造組合過濾器1,條件為 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,條件為 組合過濾器1 and 單屬性列值過濾器3,即 (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);
});
});