O método BatchWriteRow agrupa operações de gravação, atualização e exclusão de linhas em uma ou mais tabelas em uma única requisição e retorna resultados individuais por linha.
Pré-requisitos
O Tablestore SDK for Java está instalado e o cliente inicializado.
Descrição
public BatchWriteRowResponse batchWriteRow(BatchWriteRowRequest batchWriteRowRequest) throws TableStoreException, ClientException
O método batchWriteRow executa múltiplas operações de linha em uma ou mais tabelas em uma única requisição. O Tablestore processa cada linha independentemente; a falha em uma linha não afeta as demais.
Se alguma operação na requisição contiver um erro de parâmetro, o Tablestore retornará um erro de parâmetro para toda a requisição e nenhuma operação será executada.
O exemplo a seguir grava uma linha (chave primária row1) na tabela batch_write_demo, verifica o resultado geral com isAllSucceed e obtém detalhes das linhas com falha por meio de getFailedRows.
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"))
.build();
RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));
request.addRowChange(rowPutChange);
BatchWriteRowResponse response = client.batchWriteRow(request);
System.out.println("RequestId: " + response.getRequestId());
System.out.println("All Succeeded: " + response.isAllSucceed());
if (!response.isAllSucceed()) {
for (BatchWriteRowResponse.RowResult fail : response.getFailedRows()) {
System.out.println("Failed: table=" + fail.getTableName()
+ " index=" + fail.getIndex()
+ " error=" + fail.getError());
}
}
Parâmetros
|
Nome |
Tipo |
Descrição |
|
BatchWriteRowRequest (obrigatório) |
class |
Contêiner da requisição em lote. Adicione operações de linha com |
|
RowPutChange (um ou mais) |
class |
Grava (sobrescreve) uma linha inteira. Especifique o nome da tabela e a chave primária. Adicione colunas de atributo com |
|
RowUpdateChange (um ou mais) |
class |
Modifica colunas de atributo. Use |
|
RowDeleteChange (um ou mais) |
class |
Exclui uma linha inteira. Especifique o nome da tabela e a chave primária. Para mais informações, consulte Excluir uma linha. |
Exemplos
Atualização de linhas em lote
Use RowUpdateChange para atualizar colunas de atributo em uma requisição em lote.
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row_for_update"))
.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
// Update or add attribute columns
rowUpdateChange.put("col1", ColumnValue.fromString("new_val1"));
rowUpdateChange.put("col2", ColumnValue.fromLong(100));
// Delete an entire attribute column
rowUpdateChange.deleteColumns("obsolete_col");
request.addRowChange(rowUpdateChange);
client.batchWriteRow(request);
Exclusão de linhas em lote
Use RowDeleteChange para excluir linhas inteiras em uma requisição em lote.
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row_for_delete"))
.build();
RowDeleteChange rowDeleteChange = new RowDeleteChange(tableName, primaryKey);
request.addRowChange(rowDeleteChange);
client.batchWriteRow(request);
Combinação de operações entre tabelas
Combine operações de gravação, atualização e exclusão em várias tabelas em uma única requisição. O Tablestore retorna resultados individualmente para cada linha.
String tableA = "batch_write_demo";
String tableB = "batch_write_demo_2";
BatchWriteRowRequest request = new BatchWriteRowRequest();
// Insert a new row into table A
PrimaryKey pkA = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowA_new"))
.build();
RowPutChange putA = new RowPutChange(tableA, pkA);
putA.addColumn("col1", ColumnValue.fromString("valA"));
request.addRowChange(putA);
// Update an existing row in table B
PrimaryKey pkB = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowB_existing"))
.build();
RowUpdateChange updateB = new RowUpdateChange(tableB, pkB);
updateB.put("status", ColumnValue.fromString("done"));
request.addRowChange(updateB);
// Delete a row from table A
PrimaryKey pkADel = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowA_obsolete"))
.build();
RowDeleteChange deleteA = new RowDeleteChange(tableA, pkADel);
request.addRowChange(deleteA);
BatchWriteRowResponse response = client.batchWriteRow(request);
if (!response.isAllSucceed()) {
System.out.println("Failed rows: " + response.getFailedRows().size());
for (BatchWriteRowResponse.RowResult fail : response.getFailedRows()) {
System.out.println(" - table=" + fail.getTableName()
+ " index=" + fail.getIndex()
+ " error=" + fail.getError().getMessage());
}
}