首页 > 解决方案 > boto3 dynamodb batch_writer 如何执行回滚

问题描述

我正在使用 boto3 访问 AWS dynamodb 表并对其进行批量写入,这是示例代码:

with table.batch_writer() as batch:
    try:
        for i in range(10):
            id = uuid.uuid4().hex

            # A function which perform a job and finish with write to the table
            # If the job fails, the function will raise an exception
            write_to_table(batch, id)
    except:
        # Perform a rollback
        print("{} failed".format(id))

是否可以使用BatchWriteItem回滚到整个批次?(例如在 except 块中)。

谢谢。

标签: pythonamazon-web-servicesamazon-dynamodbboto3rollback

解决方案


我找到了一个解决方案,但我不确定它是否是一个好的解决方案:

batch = table.batch_writer()
try:
    for i in range(10):
        id = uuid.uuid4().hex
        write_to_table(batch, id)

    batch._flush()
except:
    print("passive rollback")

推荐阅读