Python SDK を使用して、Tablestore データテーブルの単一行を更新します。このメソッドは、属性列の値の更新、属性列の追加、属性列の特定バージョンの削除、または属性列全体の削除をサポートします。
前提条件
メソッド
def update_row(self, table_name, row, condition, return_type=None, transaction_id=None)
例
次の例では、test_table テーブルでプライマリキーの値が row1 の行を更新し、col1 の値を changed_val1 に変更します。
try:
# プライマリキーを構築します。
primary_key = [('id', 'row1')]
# 更新する属性列を構築します。
attribute_columns = {
'PUT': [('col1', 'changed_val1')]
}
# 更新する行を構築します。
row = Row(primary_key, attribute_columns)
# update_row メソッドを呼び出して行を更新します。
# 行を更新するには、条件を指定する必要があります。
# RowExistenceExpectation.IGNORE は、更新前にシステムが行の存在をチェックしないことを意味します。
consumed, return_row = client.update_row('test_table', row, Condition(RowExistenceExpectation.IGNORE))
print('読み取り CU コスト: %s' % consumed.read)
print('書き込み CU コスト: %s' % consumed.write)
except Exception as e:
print("行の更新が失敗しました。エラー: %s" % e)
必要に応じて、次の行操作を実行します:
-
属性列を追加します。
attribute_columns = { 'PUT': [('col2', 'val2')] } -
属性列のバージョン番号を設定します。
attribute_columns = { 'PUT': [('col2', 'val2', int(time.time() * 1000))] } -
属性列の特定のバージョンを削除します。
attribute_columns = { 'DELETE': [('col2', None, 1747893563831)] } -
属性列全体を削除します。
attribute_columns = { 'DELETE_ALL': ['col2'] }