Use the Tablestore Go SDK to write one row to a data table.
Prerequisites
Method
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)
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
该文章对您有帮助吗?