Read rows from one or more Tablestore tables in a single batch request.
Usage notes
A single batch request retrieves up to 100 rows.
Prerequisites
Method
def batch_get_row(self, request)
Sample code
Read two rows (row1 and row2) from test_table:
try:
primary_keys = []
# Add the primary key for the first row.
primary_keys.append([('id', 'row1')])
# Add the primary key for the second row.
primary_keys.append([('id', 'row2')])
# Construct the query condition.
batchGetRowItem = TableInBatchGetRowItem('test_table', primary_keys=primary_keys, max_version=1)
# Call the batch_get_row method to perform a batch get operation.
request = BatchGetRowRequest()
request.add(batchGetRowItem)
response = client.batch_get_row(request)
# Process the response.
print('* Is all succeeded: %s' % response.is_all_succeed())
print('* Succeeded Rows: ')
for succeed in response.get_succeed_rows():
if succeed.row is not None:
print('table_name: %s. primary_key: %s, attribute_columns: %s.' % (succeed.table_name, succeed.row.primary_key,
succeed.row.attribute_columns))
else:
print('table_name: %s. This row does not exist.' % succeed.table_name)
if not response.is_all_succeed():
print('* Failed Rows: ')
for failed in response.get_failed_rows():
print('table_name: %s. error code: %s, error message: %s.' % (failed.table_name, failed.error_code,
failed.error_message))
except Exception as e:
print("Batch get row failed with error: %s" % e)
Additional parameter examples:
-
Read from multiple tables by adding a
TableInBatchGetRowItemfor each table.# Construct the query condition for the second table. primary_keys1 = [[('order_id', '90fb478c-1360-11f0-a34d-00163e30a2a9')]] batchGetRowItem1 = TableInBatchGetRowItem('orders_small', primary_keys=primary_keys1, max_version=1) request.add(batchGetRowItem1) -
Set a version range to read only versions within that range.
# Set the version range to the last 24 hours from the current time. time_range = (int(time.time() * 1000 - 86400 * 1000), int(time.time() * 1000)) batchGetRowItem = TableInBatchGetRowItem('test_table', primary_keys=primary_keys, time_range=time_range) -
Specify which attribute columns to read.
columns_to_get = ['col1'] batchGetRowItem = TableInBatchGetRowItem('test_table', primary_keys=primary_keys, columns_to_get=columns_to_get, max_version=1)