全部产品
Search
文档中心

表格存储:过滤器

更新时间:May 11, 2026

表格存储支持在查询数据时使用过滤器在服务端按指定条件进行数据过滤,本文介绍如何在 .NET SDK 中使用过滤器。

前提条件

初始化Tablestore Client

过滤器类型

表格存储的过滤器类型包括以下 2 种。

  • 单属性列值过滤器(RelationalCondition):判断单个属性列的值是否符合条件。

  • 组合过滤器(CompositeCondition):将多个条件组合进行数据过滤。

单属性列值过滤器

public class RelationalCondition : IColumnCondition

参数说明

名称

类型

说明

Operator(必选)

CompareOperator

关系运算符,包括 EQUAL(等于)、NOT_EQUAL(不等于)、GREATER_THAN(大于)、GREATER_EQUAL(大于等于)、LESS_THAN(小于)、LESS_EQUAL(小于等于)。

ColumnName(必选)

string

判断的属性列名称。

ColumnValue(必选)

ColumnValue

判断的值。

PassIfMissing(可选)

bool

行数据不包含判断的属性列时,是否返回该行,默认值为 true,即返回该行数据。

LatestVersionsOnly(可选)

bool

是否只判断查询结果中最新的数据版本,默认值为 true,即当参考的属性列存在多个数据版本时,只判断最新的数据版本是否符合过滤条件。

示例代码

以下示例代码以范围查询为例查询主键值为 [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

参数说明

名称

类型

说明

LogicOperator(必选)

LogicOperator

逻辑运算符,包括 NOT(非)、AND(与)、OR(或)。

subConditions(必选)

List<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}");
}

相关文档