【问题标题】:An error occurred (ResourceInUseException) when calling the CreateTable operation: Table already exists:调用 CreateTable 操作时发生错误(ResourceInUseException):表已存在:
【发布时间】:2019-09-05 11:52:11
【问题描述】:

在 dynamodb 中创建表以将数据从 s3 存储桶放入并加载到具有超过 4 列的表中时出现以下错误。

cloudwatchlogs中的错误:“模块初始化错误:调用CreateTable操作时发生错误(ResourceInUseException):表已存在:” 示例代码:

import boto3
s3 = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
def txt_reader(event,context):
bucket_path = event['Records'][0]['s3']['bucket']['name']
key_path = event['Records'][0]['s3']['object']['key']
obj = s3.get_object(Bucket = bucket_path,Key = key_path)
body_rows = obj['Body'].read().decode('utf-8').split('\n')

# Create the DynamoDB table.
   table_name = dynamodb.create_table(
   TableName='TFM',
     KeySchema=[
      {
        'AttributeName': 'CN',
        'KeyType': 'HASH'
       },
    {
        'AttributeName': 'SN',
        'KeyType': 'RANGE'
    }
],
AttributeDefinitions=[
    {
        'AttributeName': 'CN',
        'AttributeType': 'S'
    },
    {
        'AttributeName': 'SN',
        'AttributeType': 'S'
    },
    {
        'AttributeName': 'WF',
        'AttributeType': 'S'
    },
],
#defining local secondary index on column WF
LocalSecondaryIndexes=[
           {
              'IndexName': 'WF',
              'KeySchema': [
                  {
                    'KeyType': 'HASH',
                    'AttributeName': 'CN'
                },
                {
                    'KeyType': 'RANGE',
                    'AttributeName': 'WF'
                }
            ],

            'Projection': {
                'ProjectionType': 'ALL',
            }
        }
    ],
ProvisionedThroughput={
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
}
)

table=dynamodb.Table(table_name)

#using a method batch_writer as batch below

with table.batch_writer() as batch:
    for row in body_rows:
        batch.put_item(Item = {
            'CN':row.split('|')[0],
            'SN':row.split('|')[1],
            'WF':row.split('|')[2],
            'sf':row.split('|')[3],
            'Con':row.split('|')[4],
            'LCI':row.split('|')[5]
            })

我的问题:请随机输入一些“|”来帮助我txt 文件中给定列的分隔值并在 lambda 中运行代码。

注意:要使用的服务是作为资源的 Dynamodb,作为客户端的 S3。在这种情况下,我收到错误,但我可以看到,每次我保存代码并在 s3 中上传 txt 文件时,也会创建表,然后出现上面给出的错误。我删除了表格,保存了 lambda 代码并在 S3 中上传了文件,我又得到了同样的错误。这里 S3 充当触发器。我还创建了一个 S3-lambda-cloudwatchlogs-dynamodb 角色。

上面已经给出了

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-dynamodb python-3.6


    【解决方案1】:

    通常,在大多数情况下,表创建是一次操作。您应该在另一个脚本中创建表,让它运行一次,然后在 lambda 上运行代码的另一部分。

    无论如何,你必须每次创建和销毁表,然后在 put_item 操作之前检查表的状态。因为一旦执行表创建 API,如果不是立即创建。它是一个创建表的请求,创建需要一段时间。这是描述 table_status 的文档,根据它可以决定何时应该运行 put_item 操作

    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.table_status

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 2020-01-16
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      • 2021-09-03
      • 2018-06-08
      相关资源
      最近更新 更多