Use Tablestore SDK for Java to add, modify, or delete attribute columns in a row identified by its complete primary key.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Description
Call updateRow to update a row identified by its complete primary key. You can add or modify attribute columns, delete a specified or all versions of a column, and use atomic counters.
public UpdateRowResponse updateRow(UpdateRowRequest updateRowRequest) throws TableStoreException, ClientException
The following example updates row row1 in the update_row_demo data table, setting attribute column col1 to changed_val1.
String tableName = "update_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
rowUpdateChange.put("col1", ColumnValue.fromString("changed_val1"));
client.updateRow(new UpdateRowRequest(rowUpdateChange));
Parameters
UpdateRowRequest contains the following parameters.
|
Name |
Type |
Description |
|
rowChange (required) |
|
The configurations for updating a row. |
|
transactionId (optional) |
|
The local transaction ID. Set this parameter only when you update data in a local transaction. For information about how to obtain and use the ID, see Use local transactions. |
Row change
rowChange is of the RowUpdateChange type and contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
|
The name of the table. |
|
primaryKey (required) |
|
The primary key of the row. Specify all primary key columns in the same order and with the same types as the table schema. Primary key columns support the |
|
columnsToUpdate (required) |
|
The attribute column changes. Call |
|
condition (optional) |
|
The update condition. By default, Tablestore does not check whether the row exists. For information about how to configure the condition, see Use conditional updates. |
|
returnType (optional) |
|
The return type. The default value is For information about how to configure atomic counters, see Use atomic counters. |
|
returnColumnNames (optional) |
|
The attribute columns whose updated values you want to return. This parameter applies only to atomic counters. |
Attribute column change methods
Call the following methods to add changes to columnsToUpdate.
-
put: Adds an attribute column or writes a new version. You can specify the data version number. -
deleteColumn: Deletes a specified version of an attribute column. -
deleteColumns: Deletes all versions of an attribute column. -
increment: Atomically increments an attribute column of theINTEGERtype.
Response
UpdateRowResponse contains the following operation-specific field.
|
Field |
Type |
Description |
|
|
|
The updated attribute columns, obtained by calling |
Scenarios
Write attribute columns with a specified version number
Pass a third argument to put to specify the version number (a millisecond timestamp) for the attribute column. Use this approach to archive historical values keyed to business time.
String tableName = "update_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
// Specify the version number (a millisecond timestamp) as the third argument.
long version = System.currentTimeMillis();
rowUpdateChange.put("col2", ColumnValue.fromString("val2"), version);
client.updateRow(new UpdateRowRequest(rowUpdateChange));
Delete a specified version of an attribute column
Use deleteColumn to remove a single version of an attribute column while keeping all other versions intact.
String tableName = "update_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
// Delete the col2 value at the specified version (a millisecond timestamp).
rowUpdateChange.deleteColumn("col2", 1747893563831L);
client.updateRow(new UpdateRowRequest(rowUpdateChange));
Delete an entire attribute column
Use deleteColumns to delete every version of an attribute column in a single operation.
String tableName = "update_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
// Delete all versions of the col2 attribute column.
rowUpdateChange.deleteColumns("col2");
client.updateRow(new UpdateRowRequest(rowUpdateChange));