本文介紹如何通過 Go SDK 在Table Store的資料表中寫入單行資料。
前提條件
方法說明
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)範例程式碼
以下範例程式碼在 test_table 表中寫入一行資料,該行資料的主索引值為 row1。
func PutRowSample(client *tablestore.TableStoreClient) {
// 構造主鍵
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
// 構造寫入行資料
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// 寫入資料時必須指定寫入條件 (RowExistenceExpectation_IGNORE,表示不做行存在性判斷)
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// 調用 PutRow 方法寫入行資料
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)
}
}添加屬性列。
putRowChange.AddColumn("col1", "val1")指定資料版本號碼,您可以為每個屬性列指定單獨的版本號碼。
putRowChange.AddColumnWithTimestamp("col1", "val1", int64(time.Now().Unix() * 1000)) putRowChange.AddColumnWithTimestamp("col2", int64(3), int64(1758249013000))