You can use match all query to match all rows in a table to query the total number of rows in the table and return multiple random rows.
Prerequisites
- The OTSClient is initialized. For more information, see Initialize an OTSClient instance.
- A data table is created. Data is written to the table.
- A search index is created for the data table. For more information, see Create search indexes.
Parameters
Parameter | Description |
query | The type of the query. Set the query parameter to MatchAllQuery. |
table_name | The name of the data table. |
index_name | The name of the search index. |
limit | The maximum number of rows that you want the current query to return. To query only the number of rows that meet the query conditions without returning specific data, you can set limit to 0. This way, Tablestore returns the number of rows that meet the query conditions without specific data from the table. |
get_total_count | Specifies whether to return the total number of rows that meet the query conditions. Default value: false. The value of false specifies that the total number of rows that meet the query conditions is not returned. If you set this parameter to true, the query performance is compromised. |
columns_to_get | Specifies whether to return all columns of each row that meets the query conditions.
|
Examples
The following sample code provides an example on how to query the total number of rows in a table:
Tablestore SDK for Python V5.2.1 or later
By default, if you use Tablestore SDK for Python V5.2.1 or later to perform a match all query, a SearchResponse object is returned. The following code provides a sample request:
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))
You can use the following sample request to return results of the Tuple type:
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))
Tablestore SDK for Python of a version earlier than V5.2.1
If you use a version of Tablestore SDK for Python that is earlier than V5.2.1 to perform a match all query, results of the TUPLE type are returned by default. The following sample code provides a sample request:
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))