本文介紹如何使用 Go SDK 刪除Table Store資料表的單行資料。
前提條件
方法說明
func (tableStoreClient *TableStoreClient) DeleteRow(request *DeleteRowRequest) (*DeleteRowResponse, error)範例程式碼
以下範例程式碼用於刪除 test_table 表中主索引值為 row1 的行資料。
func DeleteRowSample(client *tablestore.TableStoreClient) {
// 構造主鍵
deletePk := new(tablestore.PrimaryKey)
deletePk.AddPrimaryKeyColumn("id", "row1")
// 構造刪除的行資料
deleteRowChange := new(tablestore.DeleteRowChange)
deleteRowChange.TableName = "test_table"
deleteRowChange.PrimaryKey = deletePk
// 刪除行資料時必須配置刪除條件 (RowExistenceExpectation_IGNORE,表示不做行存在性判斷)
deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// 調用 DeleteRow 方法刪除行資料
deleteRowReq := new(tablestore.DeleteRowRequest)
deleteRowReq.DeleteRowChange = deleteRowChange
response, err := client.DeleteRow(deleteRowReq)
if err != nil {
fmt.Println("Delete 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)
}
}