Tablestore提供了PutRow和UpdateRow介面用於寫入單行資料以及BatchWriteRow介面用於批量寫入資料。
前提條件
- 已初始化Client,詳情請參見初始化。
- 已建立資料表並寫入資料。
插入單行資料
調用PutRow介面新寫入一行資料。如果該行已存在,則先刪除原行資料(原行的所有列以及所有版本的資料),再寫入新行資料。
介面
// @param PutRowRequest 執行PutRow操作所需參數的封裝。
// @return PutRowResponse
PutRow(request *PutRowRequest) (*PutRowResponse, error)
參數
參數 | 說明 |
---|---|
TableName | 資料表名稱。 |
PrimaryKey | 行的主鍵。
说明
|
Columns | 行的屬性列。
|
Condition | 使用條件更新,可以設定原行的存在性條件或者原行中某列的列值條件。更多資訊,請參見條件更新。 |
樣本
插入一行資料。
putRowRequest := new(tablestore.PutRowRequest)
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = tableName
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
putPk.AddPrimaryKeyColumn("pk2", int64(2))
putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
putRowChange.PrimaryKey = putPk
putRowChange.AddColumn("col1", "col1data1")
putRowChange.AddColumn("col2", int64(3))
putRowChange.AddColumn("col3", []byte("test"))
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
putRowRequest.PutRowChange = putRowChange
_, err := client.PutRow(putRowRequest)
if err != nil {
fmt.Println("putrow failed with error:", err)
} else {
fmt.Println("putrow finished")
}
詳細代碼請參見PutRow@GitHub。
更新單行資料
介面
// 更新表中的一行資料。
// @param UpdateRowRequest 執行UpdateRow操作所需參數的封裝。
// @return UpdateRowResponse UpdateRow操作的響應內容。
UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)
參數
參數 | 說明 |
---|---|
TableName | 資料表名稱。 |
PrimaryKey | 行的主鍵。
说明 設定的主鍵個數和類型必須和資料表的主鍵個數和類型一致。
|
Columns | 行的屬性列。
|
Condition | 使用條件更新,可以設定原行的存在性條件或者原行中某列的列值條件。更多資訊,請參見條件更新。 |
樣本
更新一行資料。
updateRowRequest := new(tablestore.UpdateRowRequest)
updateRowChange := new(tablestore.UpdateRowChange)
updateRowChange.TableName = tableName
updatePk := new(tablestore.PrimaryKey)
updatePk.AddPrimaryKeyColumn("pk1", "pk1value1")
updatePk.AddPrimaryKeyColumn("pk2", int64(2))
updatePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
updateRowChange.PrimaryKey = updatePk
updateRowChange.DeleteColumn("col1")
updateRowChange.PutColumn("col2", int64(77))
updateRowChange.PutColumn("col4", "newcol3")
updateRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
updateRowRequest.UpdateRowChange = updateRowChange
_, err := client.UpdateRow(updateRowRequest)
if err != nil {
fmt.Println("update failed with error:", err)
} else {
fmt.Println("update row finished")
}
詳細代碼請參見UpdateRow@GitHub。
批量寫入資料
調用BatchWriteRow介面在一次請求中進行批量的寫入操作,也支援一次對多張表進行寫入。BatchWriteRow操作由多個PutRow、UpdateRow、DeleteRow子操作組成,構造子操作的過程與使用PutRow介面、UpdateRow介面和DeleteRow介面時相同,也支援使用條件更新。
BatchWriteRow的各個子操作獨立執行,Tablestore會分別返回各個子操作的執行結果。
注意事項
- 由於批量寫入可能存在部分行失敗的情況,失敗行的Index及錯誤資訊在返回的BatchWriteRowResponse中,但並不拋出異常。因此調用BatchWriteRow介面時,需要檢查傳回值,判斷每行的狀態是否成功;如果不檢查傳回值,則可能會忽略掉部分操作的失敗。
當服務端檢查到某些操作出現參數錯誤時,BatchWriteRow介面可能會拋出參數錯誤的異常,此時該請求中所有的操作都未執行。
- BatchWriteRow操作單次支援寫入的最大行數為200行,且所有行的資料量總和不能超過4 MB。
介面
// 對多個資料表中的多行資料進行增加、刪除或者更新操作。
// @param BatchWriteRowRequest 執行BatchWriteRow操作所需參數的封裝。
// @return BatchWriteRowResponse BatchWriteRow操作的響應內容。
BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse,error)
參數
詳細參數說明請參見插入單行資料。
樣本
批量寫入100行資料。
batchWriteReq := &tablestore.BatchWriteRowRequest{}
for i := 0; i < 100; i++ {
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = tableName
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
putPk.AddPrimaryKeyColumn("pk2", int64(i))
putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
putRowChange.PrimaryKey = putPk
putRowChange.AddColumn("col1", "fixvalue")
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
batchWriteReq.AddRowChange(putRowChange)
}
response, err := client.BatchWriteRow(batchWriteReq)
if err != nil {
fmt.Println("batch request failed with:", response)
} else {
fmt.Println("batch write row finished")
}
詳細代碼請參見BatchWriteRow@GitHub。