Topik ini menjelaskan cara menggunakan Go SDK untuk menulis satu baris ke tabel di Tablestore.
Prasyarat
Metode
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)Kode contoh
Kode contoh berikut menunjukkan cara menulis baris dengan nilai kunci primer 'row1' ke tabel 'test_table'.
func PutRowSample(client *tablestore.TableStoreClient) {
// Membuat kunci primer.
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
// Membuat baris yang akan ditulis.
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// Anda harus menentukan kondisi untuk operasi penulisan. RowExistenceExpectation_IGNORE melewati pemeriksaan keberadaan baris.
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// Memanggil metode PutRow untuk menulis baris.
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)
}
}Menambahkan kolom atribut.
putRowChange.AddColumn("col1", "val1")Menentukan timestamp. Anda dapat menetapkan timestamp terpisah untuk setiap kolom atribut.
putRowChange.AddColumnWithTimestamp("col1", "val1", int64(time.Now().Unix() * 1000)) putRowChange.AddColumnWithTimestamp("col2", int64(3), int64(1758249013000))