Write a single row

更新时间:
复制 MD 格式

Use the Tablestore Go SDK to write one row to a data table.

Prerequisites

Initialize the Tablestore client

Method

func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)

PutRowRequest parameters

  • PutRowChange (Required)*PutRowChange: Row data to write. Contains the following fields.

    Parameter

    Type

    Description

    TableName (Required)

    string

    Name of the data table.

    PrimaryKey (Required)

    *PrimaryKey

    Primary key for the row: column names and values.

    • Primary key columns use STRING, INTEGER, or BINARY.

    • The number and types of primary key columns must match the table primary key definition.

    • If a primary key column is an auto-increment column, set its value to a placeholder. See Auto-increment primary key.

    Columns (Optional)

    []AttributeColumn

    Attribute columns: names, values, and version timestamps.

    • Attribute columns support STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN.

    • By default, the system generates a version timestamp. To use a custom timestamp, see Data versions and time to live.

    Condition (Required)

    *RowCondition

    Row condition for the write. See conditional update.

    ReturnType (Optional)

    ReturnType

    What the API returns after the write.

    • ReturnType_RT_NONE (default): Returns no column data.

    • ReturnType_RT_PK: Returns primary key columns—use when the table defines an auto-increment primary key column.

    • ReturnType_RT_AFTER_MODIFY: Returns column values after modification—required for atomic counter flows.

    TransactionId (Optional)

    *string

    The local transaction ID. See local transaction.

Sample code

The following example writes a row with primary key row1 to the test_table data table.

func PutRowSample(client *tablestore.TableStoreClient) {
    // Build the primary key.
    putPk := new(tablestore.PrimaryKey)
    putPk.AddPrimaryKeyColumn("id", "row1")

    // Build the row change.
    putRowChange := new(tablestore.PutRowChange)
    putRowChange.TableName = "test_table"
    putRowChange.PrimaryKey = putPk
    // Set a row condition (required). RowExistenceExpectation_IGNORE skips the existence check.
    putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)

    // Invoke PutRow.
    putRowRequest := new(tablestore.PutRowRequest)
    putRowRequest.PutRowChange = putRowChange
    response, err := client.PutRow(putRowRequest)
    if err != nil {
        fmt.Println("Put row failed with error: ", err)
    } else {
        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)
    }
}
  • Add an attribute column:

    putRowChange.AddColumn("col1", "val1")
  • Set a version timestamp per attribute column with AddColumnWithTimestamp:

    putRowChange.AddColumnWithTimestamp("col1", "val1", int64(time.Now().Unix() * 1000))
    putRowChange.AddColumnWithTimestamp("col2", int64(3), int64(1758249013000))

Related topics

Batch update data