Go SDK 可向寬表模型資料表寫入或更新一行資料。
前提條件
安裝Tablestore Go SDK並初始化用戶端。
功能說明
調用 PutRow 向指定主鍵的行寫入屬性列。如果行已存在,將新增或覆蓋相應屬性列的資料版本;可通過 Condition 控制寫入是否執行。
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)
以下樣本向 example_table 寫入主索引值為 row1 的一行資料。
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")
change := &tablestore.PutRowChange{
TableName: "example_table",
PrimaryKey: primaryKey,
}
change.AddColumn("name", "Alice")
change.AddColumn("age", int64(30))
change.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
_, err := client.PutRow(&tablestore.PutRowRequest{PutRowChange: change})
if err != nil {
log.Fatal(err)
}
參數說明
PutRowRequest 包含以下參數。
|
名稱 |
類型 |
說明 |
|
PutRowChange(必選) |
|
單行寫入配置。 |
行變更
PutRowChange 包含以下參數。
|
名稱 |
類型 |
說明 |
|
TableName(必選) |
|
資料表名稱。 |
|
PrimaryKey(必選) |
|
行主鍵。主鍵列的名稱、順序和類型必須與資料表的主鍵結構一致。讀取或操作包含自增主鍵列的行時,需提供完整主鍵。 |
|
Columns(可選) |
|
要寫入的屬性列。可調用 |
|
Condition(必選) |
|
寫入條件。可設定行存在性條件和列條件。更多資訊,請參見條件更新。 |
|
ReturnType(可選) |
|
傳回型別。預設值為 |
|
TransactionId(可選) |
|
局部事務 ID。僅在局部事務內寫入資料時設定。更多資訊,請參見局部事務。 |
屬性列
Columns[] 的類型為 AttributeColumn,包含以下參數。
|
名稱 |
類型 |
說明 |
|
ColumnName(必選) |
|
屬性列名稱。 |
|
Value(必選) |
|
屬性列值。支援字串、整數、二進位、浮點數和布爾值。 |
|
Timestamp(可選) |
|
資料版本號碼,單位為毫秒。未設定時由服務端自動產生。 |
傳回值
PutRowResponse 中的業務返回欄位如下。
|
欄位 |
類型 |
說明 |
|
|
|
寫入行的完整主鍵。僅當 |
情境樣本
指定資料版本號碼
以下樣本分別使用目前時間和指定時間戳記作為屬性列的資料版本號碼。
currentTimestamp := time.Now().UnixMilli()
change.AddColumnWithTimestamp("status", "active", currentTimestamp)
change.AddColumnWithTimestamp("score", int64(95), int64(1758249013000))