Write multiple rows to a table in a single request with the Tablestore SDK for Java. The method supports put, update, and delete operations for offline big-data scenarios.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Description
public BulkImportResponse bulkImport(BulkImportRequest bulkImportRequest) throws TableStoreException, ClientException
bulkImport submits multiple row operations in a single request. The server processes each row independently and returns per-row results. This method suits offline bulk imports in big-data scenarios.
-
Add row operations with
addRowChange(RowChange)oraddRowChanges(List<RowChange>). Both methods accept threeRowChangesubclasses:RowPutChange,RowUpdateChange, andRowDeleteChange. You can mix all three subclasses in a single request. -
Call
BulkImportResponse.getResult(succeedRows, failedRows)to populate the two lists with the per-row results. CallisAllSucceed()to check whether all rows succeeded. Each row result is independent — a failure on one row does not affect operations on other rows.
The following example inserts five rows into the bulk_import_demo table in a single bulk request and prints the count of succeeded and failed rows.
String tableName = "bulk_import_demo";
BulkImportRequest request = new BulkImportRequest(tableName);
List<RowChange> rowChanges = new ArrayList<RowChange>();
for (int i = 0; i < 5; i++) {
PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("row" + i))
.build();
RowPutChange put = new RowPutChange(tableName, pk);
put.addColumn(new Column("col1", ColumnValue.fromString("v" + i)));
rowChanges.add(put);
}
request.addRowChanges(rowChanges);
BulkImportResponse response = client.bulkImport(request);
// Retrieve per-row results into succeedRows and failedRows
List<BulkImportResponse.RowResult> succeedRows = new ArrayList<BulkImportResponse.RowResult>();
List<BulkImportResponse.RowResult> failedRows = new ArrayList<BulkImportResponse.RowResult>();
response.getResult(succeedRows, failedRows);
System.out.println("All succeed: " + response.isAllSucceed());
System.out.println("Succeed: " + succeedRows.size() + ", Failed: " + failedRows.size());
Parameters
|
Name |
Type |
Description |
|
tableName (Required) |
String |
The name of the table. |
|
rowChanges (Required) |
List<RowChange> |
The list of row operations. Each element is one of |
Examples
Combine put, update, and delete in a single request
Use one BulkImportRequest to submit put, update, and delete operations together.
String tableName = "bulk_import_demo";
BulkImportRequest request = new BulkImportRequest(tableName);
// Insert a new row
PrimaryKey pkPut = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("mixed_put"))
.build();
RowPutChange put = new RowPutChange(tableName, pkPut);
put.addColumn(new Column("col1", ColumnValue.fromString("put_value")));
request.addRowChange(put);
// Update an existing row (append a column)
PrimaryKey pkUpdate = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("row0"))
.build();
RowUpdateChange update = new RowUpdateChange(tableName, pkUpdate);
update.put(new Column("col2", ColumnValue.fromLong(100)));
request.addRowChange(update);
// Delete an existing row
PrimaryKey pkDelete = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("row1"))
.build();
RowDeleteChange delete = new RowDeleteChange(tableName, pkDelete);
request.addRowChange(delete);
BulkImportResponse response = client.bulkImport(request);
System.out.println("Mixed all succeed: " + response.isAllSucceed());
Retry failed rows
Use createRequestForRetry(failedRows) to build a new request from the failed rows. The retry contains only the failed rows, so rows that already succeeded are not re-applied.
String tableName = "bulk_import_demo";
BulkImportRequest request = new BulkImportRequest(tableName);
PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("good"))
.build();
RowPutChange row = new RowPutChange(tableName, pk);
row.addColumn(new Column("col1", ColumnValue.fromString("ok")));
request.addRowChange(row);
BulkImportResponse response = client.bulkImport(request);
List<BulkImportResponse.RowResult> succeedRows = new ArrayList<BulkImportResponse.RowResult>();
List<BulkImportResponse.RowResult> failedRows = new ArrayList<BulkImportResponse.RowResult>();
response.getResult(succeedRows, failedRows);
// If any rows failed, build a retry request that contains only the failed rows
if (!failedRows.isEmpty()) {
BulkImportRequest retryRequest = request.createRequestForRetry(failedRows);
BulkImportResponse retryResponse = client.bulkImport(retryRequest);
System.out.println("Retry all succeed: " + retryResponse.isAllSucceed());
}