このトピックでは、Tablestore のテーブルに 1 行を書き込むために Go SDK を使用する方法について説明します。
前提条件
メソッド
func (tableStoreClient *TableStoreClient) PutRow(request *PutRowRequest) (*PutRowResponse, error)サンプルコード
以下のサンプルコードでは、プライマリキー値が 'row1' の行を 'test_table' テーブルに書き込む方法を示します。
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("行の書き込みに失敗しました。エラー: ", err)
} else {
fmt.Printf("* RequestId: %s \n", response.RequestId)
fmt.Printf("* 読み取り CU コスト: %d \n", response.ConsumedCapacityUnit.Read)
fmt.Printf("* 書き込み CU コスト: %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))