Use the Python SDK to read a single row of data from a Tablestore data table.
Usage notes
When you read data, you must provide the complete primary key value, including the value of any auto-increment primary key column.
Prerequisites
Method
def get_row(self, table_name, primary_key, columns_to_get=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 a single row where the primary key value is row1.
try:
# Construct the primary key.
primary_key = [('id', 'row1')]
# Call the get_row method to read the row data.
consumed, return_row, next_token = client.get_row('test_table', primary_key)
print('Read CU Cost: %s' % consumed.read)
print('Write CU Cost: %s' % consumed.write)
print('Row Data: %s %s' % (return_row.primary_key, return_row.attribute_columns))
except Exception as e:
print("Get row failed with error: %s" % e)
-
To return only data within a specific version range, specify the
time_rangeparameter.# Set the version range for the query to the last 24 hours. time_range = (int(time.time() * 1000 - 86400 * 1000), int(time.time() * 1000)) # Call the get_row method to read the row data. consumed, return_row, next_token = client.get_row('test_table', primary_key, time_range=time_range) -
To retrieve specific attribute columns, specify the
columns_to_getparameter.columns_to_get = ['col2'] # Call the get_row method to read the row data. consumed, return_row, next_token = client.get_row('test_table', primary_key, columns_to_get=columns_to_get)