Tablestore provides the PutRow and UpdateRow operations to allow you to write a single row of data, and the BatchWriteRow operation to allow you to write multiple rows of data in a batch.
Prerequisites
- OTSClient is initialized. For more information, see Initialization.
- A data table is created, and data is written to the table.
Insert a single row of data
You can call the PutRow operation to insert a row of data. If the row exists, the PutRow operation deletes all versions of data in all columns from the existing row, and then inserts a new row.
Parameters
Parameter | Description |
---|---|
tableName | The name of the data table. |
primaryKey | The primary key of the row.
Note
|
condition | The condition that you want to configure to perform the PutRow operation. You can
configure a row existence condition or a condition based on column values. For more
information, see Configure conditional update.
Note
|
column | The attribute columns of the row.
|
Examples
- Example 1
The following code provides an example on how to write a row that contains 10 attribute columns, each of which stores data of only one version. The version numbers (timestamps) are generated by Tablestore.
private static void putRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey); // Add attribute columns. for (int i = 0; i < 10; i++) { rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(i))); } client.putRow(new PutRowRequest(rowPutChange)); }
- Example 2
The following code provides an example on how to write a row that contains 10 attribute columns, each of which stores data of three versions. In this example, you need to specify the version numbers (timestamps).
private static void putRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey); // Add attribute columns. long ts = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j)); } } client.putRow(new PutRowRequest(rowPutChange)); }
- Example 3
The following code provides an example on how to write a row that contains 10 attribute columns, each of which stores data of three versions, when the specified row does not exist. In this example, you need to specify the version numbers (timestamps).
private static void putRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey); // Specify a row existence condition that expects the specified row to not exist. rowPutChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST)); // Add attribute columns. long ts = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j)); } } client.putRow(new PutRowRequest(rowPutChange)); }
- Example 4
The following code provides an example on how to write a row that contains 10 attribute columns, each of which stores data of three versions, when the specified row exists and the value of the Col0 column is greater than 100. In this example, you need to specify the version numbers (timestamps).
private static void putRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey); // Specify a condition for the PutRow operation. In this example, a row is inserted only when the row exists and the value of the Col0 column is greater than 100. Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST); condition.setColumnCondition(new SingleColumnValueCondition("Col0", SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100))); rowPutChange.setCondition(condition); // Add attribute columns. long ts = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j)); } } client.putRow(new PutRowRequest(rowPutChange)); }
Update a single row of data
Parameters
Parameter | Description |
---|---|
tableName | The name of the data table. |
primaryKey | The primary key of the row.
Note The number and types of the primary key columns that you specify must be the same
as the actual number and types of primary key columns in the data table.
|
condition | The condition that you want to specify to perform the UpdateRow operation. You can specify a row existence condition or a condition based on column values. For more information, see Configure conditional update. |
column | The attribute column you want to update.
|
Examples
- Example 1
The following code provides an example on how to update multiple columns, delete the specified version of a column, and delete the specified column:
private static void updateRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLE_NAME, primaryKey); // Update columns. for (int i = 0; i < 10; i++) { rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i))); } // Delete a specified version of a column. rowUpdateChange.deleteColumn("Col10", 1465373223000L); // Delete a column. rowUpdateChange.deleteColumns("Col11"); client.updateRow(new UpdateRowRequest(rowUpdateChange)); }
- Example 2
The following code provides an example on how to specify a condition for the UpdateRow operation:
private static void updateRow(SyncClient client, String pkValue) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue)); PrimaryKey primaryKey = primaryKeyBuilder.build(); // Specify the name of the data table. RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLE_NAME, primaryKey); // Specify a condition for the UpdateRow operation. In this example, a row is updated only when the row exists and the value of the Col0 column is greater than 100. Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST); condition.setColumnCondition(new SingleColumnValueCondition("Col0", SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100))); rowUpdateChange.setCondition(condition); // Update columns. for (int i = 0; i < 10; i++) { rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i))); } // Delete a specified version of a column. rowUpdateChange.deleteColumn("Col10", 1465373223000L); // Delete a column. rowUpdateChange.deleteColumns("Col11"); client.updateRow(new UpdateRowRequest(rowUpdateChange)); }
Write multiple rows of data in a batch
You can call the BatchWriteRow operation to write multiple rows to one or more tables in a batch. The BatchWriteRow operation is a set of PutRow, UpdateRow, or DeleteRow operations. When you call the BatchWriteRow operation, the process of constructing the PutRow, UpdateRow, or DeleteRow operations is the same as the process of constructing the PutRow, UpdateRow, or DeleteRow operation when you call the PutRow, UpdateRow, or DeleteRow operation. BatchWriteRow supports conditional update.
If you call the BatchWriteRow operation, each PutRow, UpdateRow, or DeleteRow operation is separately performed and the response to each PutRow, UpdateRow, or DeleteRow operation is separately returned.
Usage notes
When you call the BatchWriteRow operation to write multiple rows in a batch, some rows may fail to be written. If this happens, Tablestore does not return exceptions, but returns BatchWriteRowResponse in which the indexes and error messages of the failed rows are included. Therefore, when you call the BatchWriteRow operation, you must check the return values. You can use the isAllSucceed method of BatchWriteRowResponse to check whether all rows are written. If you do not check the return values, you may ignore the rows that fail to be written.
If the server detects that invalid parameters exist in some operations, the BatchWriteRow operation may return an exception about parameter errors before the first operation in the request is performed.
Examples
The following code provides an example on how to send a BatchWriteRow request, which includes two PutRow operations, one UpdateRow operation, and one DeleteRow operation:
private static void batchWriteRow(SyncClient client) {
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
// Construct rowPutChange1.
PrimaryKeyBuilder pk1Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk1Builder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString("pk1"));
RowPutChange rowPutChange1 = new RowPutChange(TABLE_NAME, pk1Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowPutChange1.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Add rowPutChange1 to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowPutChange1);
// Construct rowPutChange2.
PrimaryKeyBuilder pk2Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk2Builder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString("pk2"));
RowPutChange rowPutChange2 = new RowPutChange(TABLE_NAME, pk2Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowPutChange2.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Add rowPutChange2 to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowPutChange2);
// Construct rowUpdateChange.
PrimaryKeyBuilder pk3Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk3Builder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString("pk3"));
RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLE_NAME, pk3Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Delete a column.
rowUpdateChange.deleteColumns("Col10");
// Add rowUpdateChange to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowUpdateChange);
// Construct rowDeleteChange.
PrimaryKeyBuilder pk4Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk4Builder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString("pk4"));
RowDeleteChange rowDeleteChange = new RowDeleteChange(TABLE_NAME, pk4Builder.build());
// Add rowDeleteChange to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowDeleteChange);
BatchWriteRowResponse response = client.batchWriteRow(batchWriteRowRequest);
System.out.println("Whether all operations are successful:" + response.isAllSucceed());
if (!response.isAllSucceed()) {
for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
System.out.println("Failed rows:" + batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
System.out.println("Cause of failures:" + rowResult.getError());
}
/**
* You can use the createRequestForRetry method to construct another request to retry the operations on failed rows. In this example, only the retry request is constructed.
* We recommend that you use the custom retry policy in Tablestore SDKs as the retry method. You can use this feature to retry failed rows after batch operations are performed. After you configure the retry policy, you do not need to add retry code to call the operation.
*/
BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
}
}
To view the detailed sample code, visit BatchWriteRow@GitHub.