Batch read rows
Use Tablestore SDK for Python to read rows by primary key from one or more tables in a single request.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Function description
Call batch_get_row to read multiple rows. The result of each row is returned independently.
def batch_get_row(self, request)
The following example reads two rows from example_table. Only the latest version of each attribute column is returned.
primary_keys = [
[("partition", "device"), ("id", 1)],
[("partition", "device"), ("id", 2)],
]
table_item = TableInBatchGetRowItem(
"example_table",
primary_keys,
max_version=1,
)
request = BatchGetRowRequest()
request.add(table_item)
response = client.batch_get_row(request)
for item in response.get_succeed_rows():
if item.row is not None:
print(item.row.primary_key, item.row.attribute_columns)
A single batch read supports up to 100 rows.
Parameters
The batch_get_row method contains the following parameter.
|
Name |
Type |
Description |
|
request (required) |
|
The batch read request. Call |
Table read configurations
Each element in request.items[] is of the TableInBatchGetRowItem type and contains the following parameters. All rows in the same element share the attribute column, data version, and filter configurations.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the table. |
|
primary_keys (required) |
|
The primary keys to read. Each primary key must include all primary key columns and match the primary key schema of the table. |
|
columns_to_get (optional) |
|
The primary key or attribute columns to return. If this parameter is omitted, entire rows are returned. |
|
column_filter (optional) |
|
The filter. For more information, see Use filters. |
|
max_version (optional) |
|
The maximum number of versions to return for each attribute column. You must specify either |
|
time_range (optional) |
|
The data version range in milliseconds. The start is inclusive, and the end is exclusive. This parameter and |
|
start_column (optional) |
|
The start attribute column for reading a wide row. |
|
end_column (optional) |
|
The end attribute column for reading a wide row. |
|
token (optional) |
|
The start position for the next page of a wide row. |
Response
batch_get_row returns a BatchGetRowResponse. Use the following methods to obtain results.
|
Method |
Return type |
Description |
|
|
|
Indicates whether all rows were read successfully. |
|
|
|
The successful results. Each item contains the table name, row data, and consumed CUs. If the row does not exist, |
|
|
|
The failed results. Each item contains the table name, error code, and error message. |
|
|
|
The per-row results for the specified table. |
Examples
Read rows from multiple tables
The following example reads two tables in one request. Each table uses a separate TableInBatchGetRowItem to configure the read.
request = BatchGetRowRequest()
request.add(
TableInBatchGetRowItem(
"example_table",
[[("partition", "device"), ("id", 1)]],
max_version=1,
)
)
request.add(
TableInBatchGetRowItem(
"another_table",
[[("partition", "order"), ("id", 1)]],
max_version=1,
)
)
response = client.batch_get_row(request)
Filter rows
The following example returns only rows whose latest value in the status attribute column is online. All primary keys for the same table share the filter.
column_filter = SingleColumnCondition(
"status",
"online",
ComparatorType.EQUAL,
pass_if_missing=False,
)
table_item = TableInBatchGetRowItem(
"example_table",
primary_keys,
column_filter=column_filter,
max_version=1,
)
request = BatchGetRowRequest()
request.add(table_item)
response = client.batch_get_row(request)