Use Tablestore SDK for Python to configure an auto-increment primary key column and obtain its generated value when you write data.
Prerequisites
Install the Tablestore SDK for Python and initialize a client. Auto-increment primary key columns require version 4.0.0 or later. We recommend that you use the latest version.
Function description
Tablestore generates a signed 64-bit integer for an auto-increment primary key column. Generated values are unique and strictly increasing within the same partition key but may not be consecutive.
Configure an auto-increment primary key column
When you create a table, set a non-partition primary key column of the INTEGER type to PK_AUTO_INCR. Each table can have at most one auto-increment primary key column. You cannot add one after the table is created.
The following example creates example_table with the partition key id and the auto-increment primary key column sequence_id.
table_meta = TableMeta(
"example_table",
[
("id", "STRING"),
("sequence_id", "INTEGER", PK_AUTO_INCR),
],
)
table_options = TableOptions()
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
client.create_table(table_meta, table_options, reserved_throughput)
Write data and obtain the generated value
When you write data, set the value of the auto-increment primary key column to PK_AUTO_INCR. To use the generated value in subsequent queries or updates, set return_type to ReturnType.RT_PK.
primary_key = [
("id", "device-001"),
("sequence_id", PK_AUTO_INCR),
]
attribute_columns = [("status", "online")]
row = Row(primary_key, attribute_columns)
_, return_row = client.put_row(
"example_table",
row,
return_type=ReturnType.RT_PK,
)
print(return_row.primary_key)
Parameters
Auto-increment primary key column configuration
The tuple in schema_of_primary_key[] that configures an auto-increment primary key column contains the following elements.
|
Name |
Type |
Description |
|
name (required) |
|
The name of the primary key column. |
|
type (required) |
|
The type of the primary key column. Set the value to |
|
option (required) |
|
The primary key option. Set the value to |
Write parameters
The put_row method contains the following parameters that are related to auto-increment primary key columns.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the table. |
|
row (required) |
|
The row to write. Set the value of the auto-increment primary key column to |
|
return_type (optional) |
|
The response type. Set the value to |
Response
The put_row method returns a tuple that contains the following information.
|
Field |
Type |
Description |
|
consumed |
|
The read and write CUs consumed by the operation. |
|
return_row |
|
If |