1

I have a list of files that should be inserted or updated in dynamodb, so I'm doing in this way:

 var batch = _dynamoDbContext.CreateBatchWrite<MyEntity>();
 batch.AddPutItems(myEntityList);
 batch.ExecuteAsync();

This works fine if DynamoDB table is empty, but sometimes I should update instead insert, but I got the following error:

 An item with the same key has already been added. Key: Amazon.DynamoDBv2.DocumentModel.Key

How can I solve it ? I need to use batch, because of performance.

Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92

1 Answers1

1

You can use transactions to do insert or updates but they are double the cost, otherwise you will need to update one by one

Here's some more info on a previous post DynamoDB Batch Update

Tom Lee
  • 26
  • 1
  • 1
  • Is possible to use TransactWriteItems with DynamoDBContext or I should use Low-Level API ? – Ronaldo Lanhellas May 20 '21 at 11:22
  • No the dynamodb context only has batchwrite not batch transactions so you'll need to use TransactWriteItemsAsync in IAmazonDynamoDB. Just be aware if any of the updates fail within your transaction then they ALL fail so if these items you're updating arent related to/depend on each other it might be best to avoid using transactions and just update one by one OR instead of updating items you could use the dbcontext batchwrite to insert brand new items but with a newer timestamp in the sortkey or something similar, then just always read the latest copies of items and clean up old ones overnight – Tom Lee May 26 '21 at 20:22