表格存储支持在查询数据时使用过滤器在服务端按指定条件进行数据过滤,本文介绍如何在 .NET SDK 中使用过滤器。
前提条件
过滤器类型
表格存储的过滤器类型包括以下 2 种。
单属性列值过滤器(RelationalCondition):判断单个属性列的值是否符合条件。
组合过滤器(CompositeCondition):将多个条件组合进行数据过滤。
单属性列值过滤器
public class RelationalCondition : IColumnCondition示例代码
以下示例代码以范围查询为例查询主键值为 [row1, row3) 的行数据,并在查询后进行过滤,返回 col1 属性列的值等于 val1 的行数据。
try
{
// 设置查询起始主键
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// 设置查询结束主键(返回结果不包含结束主键)
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 构造过滤器,条件为 col1 == "val1"
RelationalCondition relationalCondition = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// 调用 GetRange 方法读取行数据
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, relationalCondition);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// 返回结果处理
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}设置行数据不包含判断的属性列时,不返回该行。
relationalCondition.PassIfMissing = false;
组合过滤器
最多支持 32 个条件的组合。
public class CompositeCondition : IColumnCondition示例代码
以下示例代码以范围查询为例查询主键值为 [row1, row3) 的行数据,并使用组合过滤器进行数据过滤。
try
{
// 设置查询起始主键
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// 设置查询结束主键(返回结果不包含结束主键)
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 构造单属性列值过滤器1,条件为 col1 == "val1"
RelationalCondition relationalCondition1 = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// 构造单属性列值过滤器2,条件为 col2 == "val2"
RelationalCondition relationalCondition2 = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue("val2"));
// 构造组合过滤器1,条件为 col1 == "val1" or col2 == "val2"
CompositeCondition compositeCondition1 = new CompositeCondition(LogicOperator.OR);
compositeCondition1.AddCondition(relationalCondition1);
compositeCondition1.AddCondition(relationalCondition2);
// 构造单属性列值过滤器3,条件为 col3 == "val3"
RelationalCondition relationalCondition3 = new RelationalCondition("col3", CompareOperator.EQUAL, new ColumnValue("val3"));
// 构造组合过滤器2,条件为 组合过滤器1 and 单属性列值过滤器3,即 (col1 == "val1" or col2 == "val2") and col3 == "val3"
CompositeCondition compositeCondition2 = new CompositeCondition(LogicOperator.AND);
compositeCondition2.AddCondition(compositeCondition1);
compositeCondition2.AddCondition(relationalCondition3);
// 调用 GetRange 方法读取行数据
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, compositeCondition2);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// 返回结果处理
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}