Use the Tablestore SDK for Go to configure a non-partition primary key column as an auto-increment column when you create a table and obtain the server-generated value when you write data.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
To use an auto-increment primary key column, configure the column, use an auto-increment placeholder when you write a row, and obtain the generated value from the response.
Only a non-partition primary key column can be configured as an auto-increment column, and the configuration can be specified only when the table is created. The column must be of the INTEGER type. Each table can have at most one auto-increment column. The server generates signed 64-bit integers.
The values are unique and strictly increasing within the same partition key but are not necessarily consecutive. Auto-increment primary key columns and local transactions cannot be used together.
Configure an auto-increment primary key column
When you create a table, call AddPrimaryKeyColumnOption and set the primary key option to AUTO_INCREMENT.
The following sample creates the example_table table. The user_id column is the partition key, and the record_id column is an auto-increment primary key column.
tableMeta := &tablestore.TableMeta{TableName: "example_table"}
tableMeta.AddPrimaryKeyColumn("user_id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumnOption(
"record_id",
tablestore.PrimaryKeyType_INTEGER,
tablestore.AUTO_INCREMENT,
)
request := &tablestore.CreateTableRequest{
TableMeta: tableMeta,
TableOption: &tablestore.TableOption{TimeToAlive: -1, MaxVersion: 1},
ReservedThroughput: &tablestore.ReservedThroughput{},
}
_, err := client.CreateTable(request)
if err != nil {
log.Fatal(err)
}
Wait until the table is loaded before you write data.
Write data and obtain the generated value
When you write data, call AddPrimaryKeyColumnWithAutoIncrement to set the placeholder and call SetReturnPk to return the complete primary key that contains the generated value. For an auto-increment write, the row existence condition must be set to RowExistenceExpectation_IGNORE.
The following sample writes a row and obtains the server-generated record_id value.
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("user_id", "user-a")
primaryKey.AddPrimaryKeyColumnWithAutoIncrement("record_id")
change := &tablestore.PutRowChange{
TableName: "example_table",
PrimaryKey: primaryKey,
}
change.AddColumn("status", "created")
change.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
change.SetReturnPk()
response, err := client.PutRow(&tablestore.PutRowRequest{PutRowChange: change})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.PrimaryKey)
Parameters
Auto-increment primary key column
PrimaryKeySchema contains the following parameters related to auto-increment primary key columns.
|
Name |
Type |
Description |
|
Name (required) |
|
The name of the auto-increment primary key column. |
|
Type (required) |
|
Set this parameter to |
|
Option (required) |
|
Set this parameter to |
Write configuration
PutRowChange contains the following parameters related to auto-increment primary key columns.
|
Name |
Type |
Description |
|
PrimaryKey (required) |
|
The primary key of the row. Call |
|
Condition (required) |
|
The row existence condition. Set this parameter to |
|
ReturnType (optional) |
|
The return type. Default value: |
Response
The following field in PutRowResponse is directly related to auto-increment primary key columns.
|
Field |
Type |
Description |
|
|
|
The complete primary key of the written row. This field is returned only when |