I am trying to use DynamoDB operation BatchWriteItem
, wherein I want to insert multiple records into one table.
This table has one partition key and one sort key.
I am using AWS lambda and Go language.
I get the elements to be inserted into a slice.
I am following this procedure.
Create PutRequest
structure and add AttributeValues for the first record from the list.
I am creating WriteRequest
from this PutRequest
I am adding this WriteRequest
to an array of WriteRequests
I am creating BatchWriteItemInput
which consists of RequestItems
, which is basically a map of Tablename and the array of WriteRequests
.
After that I am calling BatchWriteItem
, which results into an error:
Provided list of item keys contains duplicates.
Any pointers, why this could be happening?
Avoid Duplication in DynamoDB — A Simple Design Pattern Using Lambda and Randomness. Data deduplication is a technique to solve the common problem in distributed software architecture whereas a specific data record is duplicated unintentionally in a distributed, non-locking table like DynamoDB.
By default, the DynamoDB write operations ( PutItem , UpdateItem , DeleteItem ) are unconditional: Each operation overwrites an existing item that has the specified primary key.
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can transmit up to 16MB of data over the network, consisting of up to 25 item put or delete operations.
The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items.
You've provided two or more items with identical partition/sort keys.
Per the BatchWriteItem docs, you cannot perform multiple operations on the same item in the same BatchWriteItem request.
Use batch_writer
instead of batch_write_item
:
import boto3
dynamodb = boto3.resource("dynamodb", region_name='eu-west-1')
my_table = dynamodb.Table('mirrorfm_yt_tracks')
with my_table.batch_writer(overwrite_by_pkeys=["user_id", "game_id"]) as batch:
for item in items:
batch.put_item(
Item={
'user_id': item['user_id'],
'game_id': item['game_id'],
'score': item['score']
}
)
If you don't have a sort key, overwrite_by_pkeys
can be None
This is essentially the same answer as @MiguelTrejo (thanks! +1) but simplified
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With