【发布时间】: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