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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is possible to use TransactWriteItems with DynamoDBContext or I should use Low-Level API ?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.