首页 > 解决方案 > AWS QLDB ion_document 更新

问题描述

我有使用部分 SQL 和 ion 文档将数据插入 AWS QLDB 的代码。现在想要update一个 QLDB 里面的文档,我找不到任何例子。请帮忙!

statement = 'INSERT INTO MY_TABLE'
ion_documents = loads(dumps(MY_JSON_DATA))

def query_lambda(tx_executor, query=statement, docs=ion_documents):
    return tx_executor.execute_statement(query, [docs])

def retry_lambda():
    print ('Retrying')

cursor = session.execute_lambda(query_lambda, retry_lambda)

标签: pythonamazon-qldb

解决方案


正如您所注意到的,您需要使用 PartiQL 语句来更新文档。您必须插入文档的代码片段是您更新它所需的大部分内容:您需要进行的唯一更改是您正在执行的语句。

该文档有一个 Python 教程,其中包括更新文档的示例:https ://docs.aws.amazon.com/qldb/latest/developerguide/getting-started.python.step-5.html 。

例如(来自上面的链接),以下内容将更新示例应用程序中的车辆所有者:

def update_vehicle_registration(transaction_executor, vin, document_id):
    statement = "UPDATE VehicleRegistration AS r SET r.Owners.PrimaryOwner.PersonId = ? WHERE r.VIN = ?"
    parameters = [document_id, convert_object_to_ion(vin)]
    cursor = transaction_executor.execute_statement(statement, parameters)
    try:
        print_result(cursor)
        logger.info('Successfully transferred vehicle with VIN: {} to new owner.'.format(vin))
    except StopIteration:
        raise RuntimeError('Unable to transfer vehicle, could not find registration.')

注意?as 绑定参数的使用。这些将绑定到传递给execute_statement(按相应顺序)的第二个参数的值。

以下是有关 PartiQL 更新语句的一些信息:https ://docs.aws.amazon.com/qldb/latest/developerguide/ql-reference.update.html 。语法是:

UPDATE table [ AS table_alias ] [ BY id_alias ]
SET element = data [, element = data, ... ]
[ WHERE condition ]

运行更新语句的结果将是受更新影响的文档 ID。


推荐阅读