Tablestore provides the BulkImport operation to batch write offline data to a data table 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, delete, or modify.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
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. |
Examples
The following sample code provides an example on how to batch write offline data to a data table:
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());
}
}
FAQ
References
For more information about the API operation, see BulkImport.
For more information about how to call the operation to batch import offline data, see BulkImportRequest.java and BulkImportResponse.java.
After you write data to the data table, you can read or delete data in the table based on your business requirements.
If you want to read data in the data table, you can batch read offline data and read basic data. For more information, see Batch read offline data and Read data.
If you want to delete data in the data table, you can call the DeleteRow or BatchWriteRow operation. For more information, see Delete data.
You can also use the following methods to write data to a data table:
Call the PutRow, UpdateRow, or BatchWriteRow operation. For more information, see Write data.
Use tools such as DataWorks or DataX to synchronize data from other data sources to Tablestore. For more information, see Data Integration.