This topic describes how to use Tablestore SDK for Java to update multiple rows of data in Tablestore at the same time. You can write, modify, and delete data across multiple tables in a single operation.
Usage notes
If the server detects any parameter-related errors during an operation, it raises a parameter error exception, preventing the execution of any operations within the request.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Method description
public BatchWriteRowResponse batchWriteRow(BatchWriteRowRequest batchWriteRowRequest) throws TableStoreException, ClientExceptionSample code
The following sample code demonstrates how to insert a row of data into the test_table table by using the batch data operation method.
public static void batchWriteRowExample(SyncClient client) {
// Construct a batch write row request.
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
// Add a RowPutChange.
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"))
.build();
RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey);
batchWriteRowRequest.addRowChange(rowPutChange);
// Call the batchWriteRow method to perform batch data operations.
BatchWriteRowResponse batchWriteRowResponse = client.batchWriteRow(batchWriteRowRequest);
// Return the result.
System.out.println("* RequestId: " + batchWriteRowResponse.getRequestId());
System.out.println("* Is All Succeeded: " + batchWriteRowResponse.isAllSucceed());
if(!batchWriteRowResponse.isAllSucceed()) {
System.out.println("* Failed Rows: " + batchWriteRowResponse.getFailedRows().size());
for(BatchWriteRowResponse.RowResult rowResult:batchWriteRowResponse.getFailedRows()) {
System.out.println(batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey() + " | " + rowResult.getError());
}
}
}Sample code for different types of data operations is provided below.
RowPutChange: Write row data.
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder() .addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1")) .build(); RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey); batchWriteRowRequest.addRowChange(rowPutChange);Add attribute columns when you write row data.
// Add attribute columns. rowPutChange.addColumn("col1", ColumnValue.fromString("val1")); // Add attribute columns with custom data version number. rowPutChange.addColumn("col2", ColumnValue.fromString("val2"), System.currentTimeMillis());RowUpdateChange: Update row data. You can modify attribute column values, add data columns, delete a specific version of an attribute column, or delete an entire attribute column.
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder() .addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1")) .build(); RowUpdateChange rowUpdateChange = new RowUpdateChange("test_table", primaryKey); rowUpdateChange.put("col1", ColumnValue.fromString("changed_val1")); batchWriteRowRequest.addRowChange(rowUpdateChange);Add or delete attribute columns when you update row data.
// Add attribute columns. rowUpdateChange.put("col3", ColumnValue.fromString("val3")); // Add attribute column. with custom data version number. rowUpdateChange.put("col4", ColumnValue.fromString("val4"), System.currentTimeMillis()); // Delete attribute columns. rowUpdateChange.deleteColumns("col2");RowDeleteChange: Delete row data.
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder() .addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1")) .build(); RowDeleteChange rowDeleteChange = new RowDeleteChange("test_table", primaryKey); batchWriteRowRequest.addRowChange(rowDeleteChange);