You can write multiple rows of data to a data table at the same time in big data scenarios. To write data to a data table, you must specify the complete primary key information and the attribute columns that you want to add, remove, or modify.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
A data table is created. For more information, see Create a data table.
Parameters
Parameter | Description |
tableName | The name of the data table. |
rowChanges | The list of RowChanges. Each RowChange specifies a row that you want to write to the data table. |
Example
The following sample code provides an example on how to write multiple rows of data to a data table at the same time:
private static void bulkImport(SyncClient client, String start, String end){
// Create a bulkImportRequest.
// Specify the name of the data table.
String tableName = "<TABLE_NAME>";
BulkImportRequest bulkImportRequest = new BulkImportRequest(tableName);
// Create rowChanges to specify the data that you want to write to the data table.
List<RowChange> rowChanges = new ArrayList<RowChange>();
for (Integer i = Integer.valueOf(start); i <= Integer.valueOf(end); i++){
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(String.valueOf(i)));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowPutChange rowChange = new RowPutChange(tableName,primaryKey);
rowChange.addColumn(new Column("DC1", ColumnValue.fromString(i.toString())));
rowChange.addColumn(new Column("DC2", ColumnValue.fromString(i.toString())));
rowChange.addColumn(new Column("DC3", ColumnValue.fromString(i.toString())));
rowChanges.add(rowChange);
}
bulkImportRequest.addRowChanges(rowChanges);
// Obtain bulkImportResponse.
BulkImportResponse bulkImportResponse = client.bulkImport(bulkImportRequest);
List<BulkImportResponse.RowResult> succeedRows = new ArrayList<BulkImportResponse.RowResult>();
List<BulkImportResponse.RowResult> failedRows = new ArrayList<BulkImportResponse.RowResult>();
bulkImportResponse.getResult(succeedRows, failedRows);
for (int i = 0; i < succeedRows.size(); i++){
System.out.println(succeedRows.get(i).getConsumedCapacity().getCapacityDataSize().jsonize());
}
for (int i = 0; i < failedRows.size(); i++){
System.out.println(failedRows.get(i).getError().getCode());
System.out.println(failedRows.get(i).getError().getMessage());
}
}