本文介紹如何通過 Go SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。
前提條件
方法說明
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")