This topic describes how to configure an auto-increment primary key column for a table, write data to an auto-increment column, and obtain the generated auto-increment values using the Tablestore SDK for Go.
Considerations
The auto-increment values generated for an auto-increment column are unique and strictly monotonically increasing at the partition key level, but are not guaranteed to be consecutive.
Prerequisites
Configure an auto-increment primary key column
You can set a non-partition primary key column to auto-increment when creating a table, but you cannot do so for an existing table.
Only integer non-partition primary key columns can be auto-increment columns. A table can have at most one auto-increment column, which generates 64-bit signed integer values.
Sample code
The following sample code creates a table named test_table. The primary key of the table includes the partition key id and the auto-increment column incr.
func CreateTableSample(client *tablestore.TableStoreClient) {
tableMeta := new(tablestore.TableMeta)
tableMeta.TableName = "test_table"
tableMeta.AddPrimaryKeyColumn("id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumnOption("incr", tablestore.PrimaryKeyType_INTEGER, tablestore.AUTO_INCREMENT)
tableOption := new(tablestore.TableOption)
tableOption.MaxVersion = 1
tableOption.TimeToAlive = -1
reservedThroughput := new(tablestore.ReservedThroughput)
createTableRequest := new(tablestore.CreateTableRequest)
createTableRequest.TableMeta = tableMeta
createTableRequest.TableOption = tableOption
createTableRequest.ReservedThroughput = reservedThroughput
_, err := client.CreateTable(createTableRequest)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished.")
}
}Write data
When writing data to an auto-increment column, you must set a value placeholder. To obtain the generated auto-increment value for queries and updates, set the return type of putRowChange to ReturnType_RT_PK.
Sample code
The following sample code writes a row of data to the test_table table, obtains the primary key information of the written row, and prints it.
func PutRowSample(client *tablestore.TableStoreClient) {
// Construct the primary key
putPrimaryKey := new(tablestore.PrimaryKey)
putPrimaryKey.AddPrimaryKeyColumn("id", "row1")
// Set the auto-increment column
putPrimaryKey.AddPrimaryKeyColumnWithAutoIncrement("incr")
// Construct the row data to write
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPrimaryKey
putRowChange.AddColumn("col1", "val1")
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// Set the return type to ReturnType_RT_PK to return the primary key information of the written row
putRowChange.ReturnType = tablestore.ReturnType_RT_PK
// Call the putRow method to write the row data
putRowRequest := new(tablestore.PutRowRequest)
putRowRequest.PutRowChange = putRowChange
response, err := client.PutRow(putRowRequest)
if err != nil {
fmt.Println("Failed to put row with error:", err)
} else {
// RequestId and consumed CU
fmt.Printf("RequestId: %s \n", response.RequestId)
fmt.Printf("Read CU Cost: %d \n", response.ConsumedCapacityUnit.Read)
fmt.Printf("Write CU Cost: %d \n", response.ConsumedCapacityUnit.Write)
// Get and print the returned primary key information. If the return type is not set to ReturnType_RT_PK, the primary key information is not returned by default
fmt.Println(response.PrimaryKey)
}
}