このトピックでは、Tablestore SDK for Java を使用して、Tablestore データテーブル内の単一行のデータを更新する方法について説明します。属性列の値の更新、属性列の追加、属性列の特定のバージョンの削除、または属性列全体の削除を行うことができます。
前提条件
クライアントが初期化されていること。詳細については、「Tablestore クライアントを初期化する」をご参照ください。
メソッド
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");