This topic describes how to configure an auto-increment primary key column for a data table by using Tablestore SDK for Python. It also explains how to write data to an auto-increment primary key column and retrieve the values generated for the auto-increment primary key column.
Usage notes
Tablestore SDK for Python V4.0.0 and later support the auto-increment primary key column feature.
The values of an auto-increment primary key column are unique and increase monotonically but not always continuously within a partition that shares the same partition key value.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Configure an auto-increment primary key column
You can specify a primary key column that is not the partition key as an auto-increment primary key column when you create a data table. For existing data tables, you cannot configure an auto-increment primary key column.
You can specify a primary key column that is not the partition key as an auto-increment primary key column only when the data type of the primary key column is Integer. A data table can have at most one auto-increment primary key column. The values generated for the auto-increment primary key column are 64-bit signed long integers.
Sample code
The following sample code creates a data table named test_table. The primary key of the table includes the partition key id and the auto-increment primary key column incr.
schema_of_primary_key = [('id', 'STRING'), ('incr', 'INTEGER', PK_AUTO_INCR)]
table_meta = TableMeta('test_table', schema_of_primary_key)
table_options = TableOptions()
reserved_throughput = ReservedThroughput(CapacityUnit())
try:
client.create_table(table_meta, table_options, reserved_throughput)
print("Create table succeeded.")
except Exception as e:
print("Create table failed. %s" % e)Write data
When you write data to an auto-increment primary key column, you need to only set the value of the auto-increment primary key column to a placeholder. If you want to obtain the generated value of the auto-increment primary key column for data queries and updates, you also need to set the return type of put_row to RT_PK
Sample code
The following sample code writes a row of data to the test_table table, and obtains and prints the primary key information of the written row data.
# Construct the primary key.
primary_key = [('id', 'row1'), ('incr', PK_AUTO_INCR)]
# Construct the row data to write.
attribute_columns = [('col1', 'val1')]
row = Row(primary_key, attribute_columns)
try:
# Call the putRow method to write the row data, and set the return type to RT_PK (return the primary key information of the written row data).
consumed, return_row = client.put_row('test_table', row, return_type=ReturnType.RT_PK)
# Read and write CU consumption
print('Read CU Cost: %s' % consumed.read)
print('Write CU Cost: %s' % consumed.write)
# Obtain and print the returned primary key information. If the return type is not set to RT_PK, the primary key information is not returned by default.
if return_row is not None:
print('Primary key: %s' % return_row.primary_key)
except Exception as e:
print("Write failed. %s" % e)