本文介绍如何通过 Go SDK 更新表格存储数据表中的单行数据,您可以更新属性列的值、添加属性列、删除属性列的某个版本或整个属性列。
前提条件
方法说明
func (tableStoreClient *TableStoreClient) UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)示例代码
以下示例代码用于修改 test_table 表中主键值为 row1 的行数据,将属性列 col1 的值修改为 changed_val1。
func UpdateRowSample(client *tablestore.TableStoreClient) {
// 构造主键
updatePk := new(tablestore.PrimaryKey)
updatePk.AddPrimaryKeyColumn("id", "row1")
// 构造更新行数据
updateRowChange := new(tablestore.UpdateRowChange)
updateRowChange.TableName = "test_table"
updateRowChange.PrimaryKey = updatePk
updateRowChange.PutColumn("col1", "changed_val1")
// 更新行数据时必须指定更新条件 (tablestore.RowExistenceExpectation_IGNORE,表示不做行存在性判断)
updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// 调用 UpdateRow 方法更新行数据
updateRowRequest := new(tablestore.UpdateRowRequest)
updateRowRequest.UpdateRowChange = updateRowChange
response, err := client.UpdateRow(updateRowRequest)
if err != nil {
fmt.Println("Update 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)
}
}您也可以参照示例代码进行以下行数据操作。
添加一个属性列。
updateRowChange.PutColumn("col2", "val2")设置属性列数据版本号。
updateRowChange.PutColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000)) updateRowChange.PutColumnWithTimestamp("col2", int64(1), int64(1758249013000))删除属性列指定版本的数据。
updateRowChange.DeleteColumnWithTimestamp("col2", 1747893563831)删除整个属性列数据。
updateRowChange.DeleteColumn("col2")
相关文档
该文章对您有帮助吗?