表格存储支持在查询数据时使用过滤器在服务端按指定条件进行数据过滤,本文介绍如何在 Node.js SDK 中使用过滤器。
前提条件
过滤器类型
表格存储的过滤器类型包括以下 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);
});
});