Use the Python SDK to read data within a specified primary key range from a Tablestore data table.
Prerequisites
Method
def get_range(self, table_name, direction,
inclusive_start_primary_key,
exclusive_end_primary_key,
columns_to_get=None,
limit=None,
column_filter=None,
max_version=1,
time_range=None,
start_column=None,
end_column=None,
token=None,
transaction_id=None):
Examples
This example reads all rows from test_table with primary key values greater than row1.
try:
# Set the start primary key for the query.
inclusive_start_primary_key = [('id', 'row1')]
# Set the end primary key for the query. The end primary key is exclusive.
exclusive_end_primary_key = [('id', INF_MAX)]
# Call the get_range method to query data.
consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key)
# Process the results.
print('* Read CU Cost: %s' % consumed.read)
print('* Write CU Cost: %s' % consumed.write)
print('* Rows Data:')
for row in row_list:
print(row.primary_key, row.attribute_columns)
except Exception as e:
print("Range get failed with error: %s" % e)
A single call returns at most 5,000 rows or 4 MB. When results exceed this limit, use the returned next_start_primary_key to paginate.
while True:
# Call the get_range method to query data.
consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
inclusive_start_primary_key,
exclusive_end_primary_key)
# Process the results.
print('* Read CU Cost: %s' % consumed.read)
print('* Write CU Cost: %s' % consumed.write)
print('* Rows Count: %s' % len(row_list))
print('* Rows Data:')
for row in row_list:
print(row.primary_key, row.attribute_columns)
# Set the start primary key for the next read.
if next_start_primary_key:
inclusive_start_primary_key = next_start_primary_key
else:
break
Additional query options:
-
Filter by version range.
# Set the version range for the query to the last 24 hours. time_range = (int(time.time() * 1000 - 86400 * 1000), int(time.time() * 1000)) consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD, inclusive_start_primary_key, exclusive_end_primary_key, time_range= time_range) -
Read specific columns.
columns_to_get = ['col1'] # Call the get_range method to query data. consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD, inclusive_start_primary_key, exclusive_end_primary_key, columns_to_get) -
Limit rows per request.
limit = 10 # Call the get_range method to query data. consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD, inclusive_start_primary_key, exclusive_end_primary_key, limit=limit)