MatchAllQuery可以匹配所有行,常用于查询表中数据总行数,或者随机返回几条数据。
前提条件
参数
参数 | 描述 |
---|---|
query | 设置查询类型为MatchAllQuery。 |
table_name | 数据表名称。 |
index_name | 多元索引名称。 |
limit | 本次查询需要返回的最大数量。
如果只为了获取行数,无需获取具体数据,可以设置limit=0,即不返回任意一行数据。 |
get_total_count | 是否返回匹配的总行数,默认为false,表示不返回。
返回匹配的总行数会影响查询性能。 |
columns_to_get | 是否返回所有列。
|
示例
查询表中数据的总行数。
- 5.2.1及之后版本
使用5.2.1及之后的SDK版本时,默认的返回结果为SearchResponse对象,请求示例如下:
query = MatchAllQuery() all_rows = [] next_token = None while not all_rows 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) for row in all_rows: print row print 'Total rows:', len(all_rows)
如果需要返回Tuple类型结果,您可以使用如下请求示例实现。query = MatchAllQuery() all_rows = [] next_token = None while not all_rows 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) for row in all_rows: print row print 'Total rows:', len(all_rows)
- 5.2.1之前版本
使用5.2.1之前的SDK版本时,默认的返回结果为Tuple类型,请求示例如下:
query = MatchAllQuery() all_rows = [] next_token = None while not all_rows 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) for row in all_rows: print row print 'Total rows:', len(all_rows)