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
The following example reads all rows from the test_table 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 range scan can return a maximum of 5,000 rows or 4 MB of data. If the result set exceeds this limit, the response includes a token (next_start_primary_key) for retrieving the next batch. Use this token to iterate through the results, as shown in the following example.
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
Customize the query with the following options:
-
Set a version range to return only data within that 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) -
Specify the attribute columns to read.
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) -
Set the maximum number of rows to return in a single 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)