BatchWriteRow groups row write, update, and delete operations across one or more tables into a single request and returns per-row results.
Prerequisites
Tablestore SDK for Java is installed and the client is initialized.
Description
public BatchWriteRowResponse batchWriteRow(BatchWriteRowRequest batchWriteRowRequest) throws TableStoreException, ClientException
The batchWriteRow method runs multiple row operations across one or more tables in a single request. Tablestore processes each row independently — a single row failure does not affect the other rows.
If any operation in the request contains a parameter error, Tablestore returns a parameter error for the entire request and no operations run.
The following example writes one row (primary key row1) to the table batch_write_demo, checks the overall result with isAllSucceed, and retrieves failed-row details with 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());
}
}
Parameters
|
Name |
Type |
Description |
|
BatchWriteRowRequest (required) |
class |
The batch request container. Add row operations with |
|
RowPutChange (one or more) |
class |
Writes (overwrites) an entire row. Specify the table name and primary key. Add attribute columns with |
|
RowUpdateChange (one or more) |
class |
Modifies attribute columns. Use |
|
RowDeleteChange (one or more) |
class |
Deletes an entire row. Specify the table name and primary key. For more information, see Delete a row. |
Examples
Batch update rows
Use RowUpdateChange to update attribute columns in a batch request.
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);
Batch delete rows
Use RowDeleteChange to delete entire rows in a batch request.
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);
Mix operations across tables
Mix write, update, and delete operations across multiple tables in a single request. Tablestore returns results per row.
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());
}
}