过滤器(Filter)可以在服务器端对读取的结果再进行一次过滤,根据Filter中的条件决定返回哪些行或列。Filter可以用于GetRow、BatchGetRow和GetRange接口。

过滤器类型

目前表格存储支持以下两种Filter,这两种Filter都是基于参考列的列值决定是否过滤某行。
  • RelationalCondition:只判断某个参考列的列值。
  • CompositeCondition:对多个参考列的列值的判断结果进行逻辑组合,决定最终是否过滤。
说明 Filter是对读取后的结果再进行一次过滤,所以Filter中的参考列必须在读取的结果内。如果指定了要读取的列,且其中不包含参考列,则Filter无法获得这些参考列的值。当某个参考列不存在时,RelationalCondition的PassIfMissing参数决定此时是否满足条件,即可以选择当参考列不存在时的行为。

示例

  • 构造RelationalCondition
    public void GetRowWithRelationalCondition(OTSClient otsClient)
    {
        // 定义行的主键,必须与创建表时的TableMeta中定义的一致
        PrimaryKey primaryKey = new PrimaryKey
        {
            { "pk0", new ColumnValue(0) },
            { "pk1", new ColumnValue("abc") }
        };
    
        var rowQueryCriteria = new SingleRowQueryCriteria(TableName)
        {
            RowPrimaryKey = primaryKey
        };
    
        // 只返回col0的值等于5的行
        var filter = new RelationalCondition("col0",CompareOperator.EQUAL,new ColumnValue(5))
        {
            PassIfMissing = true
        };
    
        rowQueryCriteria.Filter = filter.ToFilter();
        rowQueryCriteria.AddColumnsToGet("col0");
        rowQueryCriteria.AddColumnsToGet("col1");
    
        GetRowRequest request = new GetRowRequest(rowQueryCriteria); 
    
        // 查询
        GetRowResponse response = otsClient.GetRow(request);
        PrimaryKey primaryKeyRead = response.PrimaryKey;
        AttributeColumns attributesRead = response.Attribute;
    
        Console.WriteLine("Primary key read: ");
        foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
        {
            Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
        }
    
        Console.WriteLine("Attributes read: ");
        foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
        {
            Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
        }
    
        Console.WriteLine("Get row with filter succeed.");
    }
  • 构造CompositeCondition
    public void GetRowWithCompositeCondition(OTSClient otsClient)
    {
        // 定义行的主键,必须与创建表时的TableMeta中定义的一致
        PrimaryKey primaryKey = new PrimaryKey
        {
            { "pk0", new ColumnValue(0) },
            { "pk1", new ColumnValue("abc") }
        };
    
        var rowQueryCriteria = new SingleRowQueryCriteria(TableName)
        {
            RowPrimaryKey = primaryKey
            };
    
        // 只返回col0的值等于5的行或者col1不等于ff的行
        var filter1 = new RelationalCondition("col0",
                                              CompareOperator.EQUAL,
                                              new ColumnValue(5));
    
        var filter2 = new RelationalCondition("col1", CompareOperator.NOT_EQUAL, new ColumnValue("ff"));
    
        var filter = new CompositeCondition(LogicOperator.OR);
        filter.AddCondition(filter1);
        filter.AddCondition(filter2);
    
        rowQueryCriteria.Filter = filter.ToFilter();
        rowQueryCriteria.AddColumnsToGet("col0");
        rowQueryCriteria.AddColumnsToGet("col1");
    
        GetRowRequest request = new GetRowRequest(rowQueryCriteria); 
    
        // 查询
        GetRowResponse response = otsClient.GetRow(request);
        PrimaryKey primaryKeyRead = response.PrimaryKey;
        AttributeColumns attributesRead = response.Attribute;
    
        Console.WriteLine("Primary key read: ");
        foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
        {
            Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
        }
    
        Console.WriteLine("Attributes read: ");
        foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
        {
            Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
        }
    
        Console.WriteLine("Get row with filter succeed.");
    }