文档

全匹配查询

更新时间:

MatchAllQuery可以匹配所有行,常用于查询表中数据总行数,或者随机返回几条数据。

前提条件

  • 已初始化OTSClient。具体操作,请参见初始化OTSClient
  • 已创建数据表并写入数据。
  • 已在数据表上创建多元索引。具体操作,请参见创建多元索引

参数

参数

描述

query

设置查询类型为MatchAllQuery。

table_name

数据表名称。

index_name

多元索引名称。

limit

本次查询需要返回的最大数量。

如果只为了获取行数,无需获取具体数据,可以设置limit=0,即不返回任意一行数据。

get_total_count

是否返回匹配的总行数,默认为false,表示不返回。

返回匹配的总行数会影响查询性能。

columns_to_get

是否返回所有列。

  • 当设置return_type为ColumnReturnType.SPECIFIED时,可以通过column_names指定返回的列。

  • 当设置return_type为ColumnReturnType.ALL时,表示返回所有列。

  • 当设置return_type为ColumnReturnType.NONE时,表示不返回所有列,只返回主键列。

示例

以下示例用于查询表中数据的总行数。

  • 5.2.1及之后版本

    使用5.2.1及之后的SDK版本时,默认的返回结果为SearchResponse对象,请求示例如下:

    query=MatchAllQuery()
    all_rows=[]
    next_token=None
    first_page=True
    
    while first_page or next_token:
        search_response=client.search(table_name, index_name,
            SearchQuery(query,next_token=next_token,limit=100,get_total_count=True),
            columns_to_get=ColumnsToGet(['k','t','g','ka','la'],ColumnReturnType.SPECIFIED))
        all_rows.extend(search_response.rows)
        first_page=False
    for row in all_rows:
        print(row)
    
    print('Totalrows:', len(all_rows))
    

    如果需要返回Tuple类型结果,您可以使用如下请求示例实现。

    query=MatchAllQuery()
    all_rows=[]
    next_token=None
    first_page=True
    
    while first_page or next_token:
        rows, next_token, total_count, is_all_succeed, agg_results, group_by_results =client.search(table_name, index_name,
            SearchQuery(query,next_token=next_token,limit=100,get_total_count=True),
            columns_to_get=ColumnsToGet(['k','t','g','ka','la'],ColumnReturnType.SPECIFIED)).v1_response()
        all_rows.extend(rows)
        first_page=False
    for row in all_rows:
        print(row)
    
    print('Totalrows:', len(all_rows))
    
  • 5.2.1之前版本

    使用5.2.1之前的SDK版本时,默认的返回结果为Tuple类型,请求示例如下:

    query=MatchAllQuery()
    all_rows=[]
    next_token=None
    first_page=True
    while first_page or next_token:
        rows, next_token, total_count, is_all_succeed = client.search(table_name, index_name,
            SearchQuery(query, next_token=next_token, limit=100, get_total_count=True),
            columns_to_get=ColumnsToGet(['k', 't', 'g', 'ka', 'la'], ColumnReturnType.SPECIFIED))
        all_rows.extend(rows)
        first_page=False
    for row in all_rows:
        print(row)
    
    print('Total rows:', len(all_rows))
  • 本页导读 (1)
文档反馈