Topik ini menjelaskan cara menggunakan Go SDK untuk memperbarui satu baris dalam tabel Tablestore. Anda dapat menambahkan atau memperbarui kolom atribut, menghapus versi tertentu dari kolom atribut, atau menghapus seluruh kolom atribut.
Prasyarat
Metode
func (tableStoreClient *TableStoreClient) UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)Kode contoh
Kode contoh berikut memperbarui satu baris dalam tabel test_table. Untuk baris dengan nilai kunci primer row1, nilai kolom atribut col1 diubah menjadi changed_val1.
func UpdateRowSample(client *tablestore.TableStoreClient) {
// Membuat kunci primer.
updatePk := new(tablestore.PrimaryKey)
updatePk.AddPrimaryKeyColumn("id", "row1")
// Membuat data pembaruan baris.
updateRowChange := new(tablestore.UpdateRowChange)
updateRowChange.TableName = "test_table"
updateRowChange.PrimaryKey = updatePk
updateRowChange.PutColumn("col1", "changed_val1")
// Kondisi wajib ditentukan saat memperbarui baris.
// tablestore.RowExistenceExpectation_IGNORE berarti tidak dilakukan pemeriksaan keberadaan baris.
updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// Memanggil metode UpdateRow untuk memperbarui baris.
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)
}
}Anda juga dapat melakukan operasi berikut terhadap baris:
Tambahkan kolom atribut.
updateRowChange.PutColumn("col2", "val2")Tetapkan timestamp (versi) untuk kolom atribut.
updateRowChange.PutColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000)) updateRowChange.PutColumnWithTimestamp("col2", int64(1), int64(1758249013000))Hapus versi tertentu dari kolom atribut.
updateRowChange.DeleteColumnWithTimestamp("col2", 1747893563831)Hapus seluruh kolom atribut.
updateRowChange.DeleteColumn("col2")