このトピックでは、Go SDK を使用して Tablestore でバッチ操作 (データの書き込み、更新、削除) を実行する方法について説明します。単一のリクエストで、複数のテーブルの行を対象にできます。
注意事項
サーバーがリクエスト内のいずれかの操作でパラメーターエラーを検出した場合、エラーが返され、どの操作も実行されません。
1 回のバッチデータ操作に含めることができる行数は最大 200 行です。すべての行の合計サイズは 4 MB を超えることはできません。
前提条件
メソッド
func (tableStoreClient *TableStoreClient) BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse, error)コード例
次のコード例は、バッチデータ操作を使用して test_table テーブルに行を挿入する方法を示しています。
func BatchWriteRowSample(client *tablestore.TableStoreClient) {
// リクエストを構築します。
batchWriteRequest := new(tablestore.BatchWriteRowRequest)
// PutRowChange を追加します。
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)
batchWriteRequest.AddRowChange(putRowChange)
// batchWriteRow メソッドを呼び出してバッチ操作を実行します。
response, err := client.BatchWriteRow(batchWriteRequest)
// レスポンスを処理します。
if err != nil {
fmt.Println("Batch write row failed with error:", err)
} else {
fmt.Printf("RequestId: %s \n", response.RequestId)
for tableName, rows := range response.TableToRowsResult {
for _, row := range rows {
if !row.IsSucceed {
fmt.Printf("TableName: %s. Failed Rows: %v \n", tableName, row.Error)
}
}
}
}
}以下のサンプルは、さまざまな種類のデータ操作を示しています。
PutRowChange: 行を書き込みます。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) batchWriteRequest.AddRowChange(putRowChange)行を書き込む際に属性列を追加します。
// 属性列を追加します。 putRowChange.AddColumn("col1", "val1") // カスタムデータバージョン番号を持つ属性列を追加します。 putRowChange.AddColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000))UpdateRowChange: 行を更新します。属性列の値を変更したり、属性列を追加したり、属性列全体またはその特定のバージョンを削除したりできます。updatePk := new(tablestore.PrimaryKey) updatePk.AddPrimaryKeyColumn("id", "row1") updateRowChange := new(tablestore.UpdateRowChange) updateRowChange.TableName = "test_table" updateRowChange.PrimaryKey = updatePk updateRowChange.PutColumn("col1", "changed_val1") // 操作の条件を設定する必要があります。RowExistenceExpectation_IGNORE は行の存在チェックをバイパスします。 updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(updateRowChange)更新中に属性列を追加または削除することもできます。
// 属性列を追加します。 updateRowChange.PutColumn("col3", "val3") // カスタムデータバージョン番号を持つ属性列を追加します。 updateRowChange.PutColumnWithTimestamp("col4", "val4", int64(time.Now().Unix() * 1000)) // 属性列を削除します。 updateRowChange.DeleteColumn("col2")DeleteRowChange: 行を削除します。deletePk := new(tablestore.PrimaryKey) deletePk.AddPrimaryKeyColumn("id", "row1") deleteRowChange := new(tablestore.DeleteRowChange) deleteRowChange.TableName = "test_table" deleteRowChange.PrimaryKey = deletePk // 操作の条件を設定する必要があります。RowExistenceExpectation_IGNORE は行の存在チェックをバイパスします。 deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(deleteRowChange)