本文介紹如何通過 Java SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。
前提條件
方法說明
public UpdateRowResponse updateRow(UpdateRowRequest updateRowRequest) throws TableStoreException, ClientException範例程式碼
以下範例程式碼用於修改 test_table 表中主索引值為 row1 的行資料,將屬性列 col1 的值修改為 changed_val1。
public static void updateRowExample(SyncClient client) {
// 構造主鍵
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// 構造更新行資料
RowUpdateChange rowUpdateChange = new RowUpdateChange("test_table", primaryKey);
rowUpdateChange.put("col1", ColumnValue.fromString("changed_val1"));
// 調用 updateRow 方法更新行資料
UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);
UpdateRowResponse updateRowResponse = client.updateRow(updateRowRequest);
// 返回結果處理
System.out.println("RequestId: " + updateRowResponse.getRequestId());
System.out.println("Read CU Cost: " + updateRowResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
System.out.println("Write CU Cost: " + updateRowResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());
}您也可以參照範例程式碼進行以下行資料操作。
添加一個屬性列
rowUpdateChange.put("col2", ColumnValue.fromString("val2"));設定屬性列資料版本號碼
rowUpdateChange.put("col2", ColumnValue.fromString("val2"), System.currentTimeMillis());刪除屬性列指定版本的資料
rowUpdateChange.deleteColumn("col2", 1747893563831L);刪除整個屬性列資料
rowUpdateChange.deleteColumns("col2");