首页 > 解决方案 > 等待dynamodb创建表,避免Requested resource not found

问题描述

如何在开始插入元素之前等待创建 dynamodb 实例表?

    dynamodb = boto3.client('dynamodb', region_name="eu-west-3")
    dynamodb.create_table(
            TableName='mytable',
            KeySchema=[
                {
                    'AttributeName': 'id',
                    'KeyType': 'HASH'  # Partition key
                }
                # no sort key
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'id',
                    'AttributeType': 'S'
                },
            ],
            BillingMode='PAY_PER_REQUEST',
        )

当我开始我的批处理时,我收到因为表尚未创建:

botocore.errorfactory.ResourceNotFoundException:
An error occurred (ResourceNotFoundException) when calling the BatchWriteItem operation:
Requested resource not found

我想等待表的创建结束。怎么做?

标签: pythonamazon-dynamodbboto3

解决方案


您可以使用它来等待表的创建结束:

waiter = dynamodb.get_waiter('table_exists')
waiter.wait(TableName='mytable')
# at this line your table is fully created and available

如文档中所述:https ://boto3.amazonaws.com/v1/documentation/api/latest/guide/clients.html#waiters


推荐阅读