Use the Tablestore SDK for Go to write or update a row in a Wide Column model table.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
Call PutRow to write attribute columns to a row identified by its primary key. If the row exists, new data versions are added or overwritten for the specified attribute columns. You can use Condition to control whether the write is performed.
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)
The following sample writes a row whose primary key value is row1 to the example_table table.
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")
change := &tablestore.PutRowChange{
TableName: "example_table",
PrimaryKey: primaryKey,
}
change.AddColumn("name", "Alice")
change.AddColumn("age", int64(30))
change.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
_, err := client.PutRow(&tablestore.PutRowRequest{PutRowChange: change})
if err != nil {
log.Fatal(err)
}
Parameters
PutRowRequest contains the following parameter.
|
Name |
Type |
Description |
|
PutRowChange (required) |
|
The row write configuration. |
Row change
PutRowChange contains the following parameters.
|
Name |
Type |
Description |
|
TableName (required) |
|
The table name. |
|
PrimaryKey (required) |
|
The primary key of the row. The names, order, and types of the primary key columns must match the table schema. To read or operate on a row that contains an auto-increment primary key column, specify the complete primary key. |
|
Columns (optional) |
|
The attribute columns to write. Call |
|
Condition (required) |
|
The write condition, which can include a row existence condition and a column condition. For more information, see Use conditional updates. |
|
ReturnType (optional) |
|
The return type. Default value: |
|
TransactionId (optional) |
|
The local transaction ID. Specify this parameter only for a write in a local transaction. For more information, see Use local transactions. |
Attribute column
Each element in Columns is of the AttributeColumn type and contains the following parameters.
|
Name |
Type |
Description |
|
ColumnName (required) |
|
The attribute column name. |
|
Value (required) |
|
The attribute column value. String, integer, binary, floating-point, and Boolean values are supported. |
|
Timestamp (optional) |
|
The data version, in milliseconds. If this parameter is not specified, the server generates the version. |
Response
PutRowResponse contains the following business field.
|
Field |
Type |
Description |
|
|
|
The complete primary key of the written row. This field is returned only when |
Examples
Specify data versions
The following sample uses the current time and a specified timestamp as data versions for attribute columns.
currentTimestamp := time.Now().UnixMilli()
change.AddColumnWithTimestamp("status", "active", currentTimestamp)
change.AddColumnWithTimestamp("score", int64(95), int64(1758249013000))