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

过滤器类型

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

示例

  • 构造SingleColumnCondition
    def get_row_with_condition(client):
        primary_key = [('uid',1), ('gid',101)]
        columns_to_get = [] # given a list of columns to get, or empty list if you want to get entire row.
        // 设置过滤器,当name的值为'杭州'时,返回该行.
        cond = SingleColumnCondition("name", '杭州', ComparatorType.EQUAL, pass_if_missing = True)
        consumed, return_row, next_token = client.get_row(table_name, primary_key, columns_to_get, cond, 1)
    
        print ('Read succeed, consume %s read cu.' % consumed.read)
    
        print ('Value of primary key: %s' % return_row.primary_key)
        print ('Value of attribute: %s' % return_row.attribute_columns)
        for att in return_row.attribute_columns:
            print ('name:%s\tvalue:%s\ttimestamp:%d' % (att[0], att[1], att[2]))
  • 构造CompositeColumnCondition
    def get_row_with_composite_condition(client):
        primary_key = [('uid',1), ('gid',101)]
        columns_to_get = [] # given a list of columns to get, or empty list if you want to get entire row.
        // 过滤器条件为 (growth == 0.9) AND (name == '杭州')
        cond = CompositeColumnCondition(LogicalOperator.AND)
        cond.add_sub_condition(SingleColumnCondition("growth", 0.9, ComparatorType.NOT_EQUAL))
        cond.add_sub_condition(SingleColumnCondition("name", '杭州', ComparatorType.EQUAL))
    
        consumed, return_row, next_token = client.get_row(table_name, primary_key, columns_to_get, cond, 1)
    
        print ('Read succeed, consume %s read cu.' % consumed.read)
    
        print ('Value of primary key: %s' % return_row.primary_key)
        print ('Value of attribute: %s' % return_row.attribute_columns)
        for att in return_row.attribute_columns:
            print ('name:%s\tvalue:%s\ttimestamp:%d' % (att[0], att[1], att[2]))