本文介绍如何通过 Go SDK 在表格存储的数据表中写入单行数据。
前提条件
方法说明
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)示例代码
以下示例代码在 test_table 表中写入一行数据,该行数据的主键值为 row1。
func PutRowSample(client *tablestore.TableStoreClient) {
// 构造主键
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
// 构造写入行数据
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// 写入数据时必须指定写入条件 (RowExistenceExpectation_IGNORE,表示不做行存在性判断)
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// 调用 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)
}
}添加属性列。
putRowChange.AddColumn("col1", "val1")指定数据版本号,您可以为每个属性列指定单独的版本号。
putRowChange.AddColumnWithTimestamp("col1", "val1", int64(time.Now().Unix() * 1000)) putRowChange.AddColumnWithTimestamp("col2", int64(3), int64(1758249013000))
相关文档
该文章对您有帮助吗?