A operação BatchWriteRow do Tablestore Go SDK insere, atualiza ou exclui várias linhas em uma ou mais tabelas em uma única solicitação.
Observações importantes
Se alguma operação na solicitação tiver parâmetros inválidos, o servidor retornará um erro de parâmetro e não executará nenhuma operação dessa solicitação.
Cada solicitação de atualização em lote aceita no máximo 200 linhas. O tamanho total de todas as linhas na solicitação não pode exceder 4 MB.
Pré-requisitos
Método e parâmetros
func (tableStoreClient *TableStoreClient) BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse, error)
Exemplos de código
Este exemplo chama BatchWriteRow para inserir uma linha na tabela test_table.
func BatchWriteRowSample(client *tablestore.TableStoreClient) {
// Construct the request.
batchWriteRequest := new(tablestore.BatchWriteRowRequest)
// Add a PutRowChange.
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check.
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
batchWriteRequest.AddRowChange(putRowChange)
// Call the batchWriteRow method to perform the batch operation.
response, err := client.BatchWriteRow(batchWriteRequest)
// Process the response.
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)
}
}
}
}
}
Os exemplos a seguir demonstram o uso individual de PutRowChange, UpdateRowChange e DeleteRowChange.
-
PutRowChange: insere ou substitui uma linha (put).putPk := new(tablestore.PrimaryKey) putPk.AddPrimaryKeyColumn("id", "row1") putRowChange := new(tablestore.PutRowChange) putRowChange.TableName = "test_table" putRowChange.PrimaryKey = putPk // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(putRowChange)Para adicionar colunas de atributo em uma operação de inserção:
// Add an attribute column. putRowChange.AddColumn("col1", "val1") // Add an attribute column with a custom data version number. putRowChange.AddColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000)) -
UpdateRowChange: atualiza uma linha ao alterar valores de atributos, adicionar colunas ou excluir uma coluna ou versão específica.updatePk := new(tablestore.PrimaryKey) updatePk.AddPrimaryKeyColumn("id", "row1") updateRowChange := new(tablestore.UpdateRowChange) updateRowChange.TableName = "test_table" updateRowChange.PrimaryKey = updatePk updateRowChange.PutColumn("col1", "changed_val1") // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(updateRowChange)Para adicionar ou excluir colunas de atributo na mesma atualização:
// Add an attribute column. updateRowChange.PutColumn("col3", "val3") // Add an attribute column with a custom data version number. updateRowChange.PutColumnWithTimestamp("col4", "val4", int64(time.Now().Unix() * 1000)) // Delete an attribute column. updateRowChange.DeleteColumn("col2") -
DeleteRowChange: exclui uma linha pela chave primária.deletePk := new(tablestore.PrimaryKey) deletePk.AddPrimaryKeyColumn("id", "row1") deleteRowChange := new(tablestore.DeleteRowChange) deleteRowChange.TableName = "test_table" deleteRowChange.PrimaryKey = deletePk // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(deleteRowChange)