This topic describes how to use Tablestore SDK for Go to add an auto-increment primary key column and to write data when you create a table.

Note
  • If you set a primary key column of a table to an auto-increment primary key column, you do not need to enter values in this column when you write data in a row. Instead, Tablestore automatically generates primary key values.
  • The values of the auto-increment primary key column are unique and increase sequentially.
  • Only one primary key column can be set to the auto-increment primary key column in each table.
  • Partition keys and attribute columns cannot be set to auto-increment columns.

Create tables

You can add an auto-increment primary key column when you create a table. Example:

import (
    "fmt"
    "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)

func CreateTableKeyAutoIncrementSample(client *tablestore.TableStoreClient) {
    createtableRequest := new(tablestore.CreateTableRequest)
    // Create a table that contains three primary key columns: pk1 of the STRING type, pk2 of the INTEGER type, and pk3 of the BINARY type. pk2 is the auto-increment primary key column.    tableMeta := new(tablestore.TableMeta)
    tableMeta.TableName = "incrementsampletable"
    tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
    tableMeta.AddPrimaryKeyColumnOption("pk2", tablestore.PrimaryKeyType_INTEGER, tablestore.AUTO_INCREMENT)
    tableMeta.AddPrimaryKeyColumn("pk3", tablestore.PrimaryKeyType_BINARY)
    tableOption := new(tablestore.TableOption)
    tableOption.TimeToAlive = -1
    tableOption.MaxVersion = 3
    reservedThroughput := new(tablestore.ReservedThroughput)
    reservedThroughput.Readcap = 0
    reservedThroughput.Writecap = 0
    createtableRequest.TableMeta = tableMeta
    createtableRequest.TableOption = tableOption
    createtableRequest.ReservedThroughput = reservedThroughput

    client.CreateTable(createtableRequest)
}

Write data

Examples:

import (
    "fmt"
    "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)

func PutRowWithKeyAutoIncrementSample(client *tablestore.TableStoreClient) {
    fmt.Println("begin to put row")
    putRowRequest := new(tablestore.PutRowRequest)
    putRowChange := new(tablestore.PutRowChange)
    putRowChange.TableName = "incrementsampletable"
    // Specify primary key columns, which must be added in the order specified when the table is created. Specify pk2 as the auto-increment primary key column.    putPk := new(tablestore.PrimaryKey)
    putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
    putPk.AddPrimaryKeyColumnWithAutoIncrement("pk2")
    putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
    putRowChange.PrimaryKey = putPk
    putRowChange.AddColumn("col1", "col1data1")
    putRowChange.AddColumn("col2", int64(100))
    putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    putRowRequest.PutRowChange = putRowChange
    _, err := client.PutRow(putRowRequest)

    if err != nil {
        fmt.Println("put row failed with error:", err)
    } else {
        fmt.Println("put row finished")
    }
}