Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB BatchWriteItem: Provided list of item keys contains duplicates

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.

  1. Create PutRequest structure and add AttributeValues for the first record from the list.

  2. I am creating WriteRequest from this PutRequest

  3. I am adding this WriteRequest to an array of WriteRequests

  4. 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?

like image 642
Dattatray Avatar asked Jun 17 '19 14:06

Dattatray


People also ask

How does DynamoDB prevent duplicate records?

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.

Does DynamoDB overwrite?

By default, the DynamoDB write operations ( PutItem , UpdateItem , DeleteItem ) are unconditional: Each operation overwrites an existing item that has the specified primary key.

What is batch write DynamoDB?

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.

What is the API call to retrieve multiple items from a DynamoDB table?

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.


2 Answers

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.

like image 105
jarmod Avatar answered Sep 23 '22 09:09

jarmod


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

like image 20
Stéphane Bruckert Avatar answered Sep 21 '22 09:09

Stéphane Bruckert