Question

From AWS docs:

Note
BatchWriteItem cannot update items. To update items, use the UpdateItem API.

But, what is not clear from the docs here is what they mean by "To update items". If the items in PutRequests are existing items, will they be replaced as expected in a normal PutItem request, or will they throw an exception?

Was it helpful?

Solution 2

I guess the following example will help.

assume this query:

$response = $client->batchWriteItem(array(
"RequestItems" => array(
    "user" => array(
        array(
            "PutRequest" => array(
                "Item" => array(
                    "userId" => array(Type::NUMBER => 7),
                    "attr1" => array(Type::NUMBER => 1),
                    "attr2" => array(Type::NUMBER => 2),
                    "attr3" => array(Type::NUMBER => 3), 
                )
            ),
        )))));

This will create an Item with userId, attr1, attr2, attr3. Lets say you execute following batchWrite

$response = $client->batchWriteItem(array(
"RequestItems" => array(
    "user" => array(
        array(
            "PutRequest" => array(
                "Item" => array(
                    "userId" => array(Type::NUMBER => 7),
                    "temp1" => array(Type::NUMBER => 11),
                    "temp2" => array(Type::NUMBER => 22),
                )
            ),
        )))));

After this query is executed Item(row) would be userId, temp1, temp2 instead of userId,temp1,temp2,attr1,attr2,attr3.

So Technically they are right that we cannot use to update the item because it will create new item with same Hash Key replacing old Item(row).

so if you want to update the Item then we can use

            $params['temp1'] = array(
                'Action' => 'PUT',
                'Value' => array(
                    Type::NUMBER => 555
                )
            );
        }

            $response = $client->updateItem(array(
                "TableName" => "user",
                "Key" => array(
                    "userId" => array(
                        Type::NUMBER => 7
                    )
                ),
                "AttributeUpdates" => $params
                    )
            );

This will update the item(row) and change value of temp1 from 1 to 555

Hope this will help

OTHER TIPS

They mean you don't have an updateItem action within batchWriteItems, so yes you can only "put" items as in a normal put, if the item exists it will be replaced. Keep in mind that batchWriteItem is not throttled so, if your application is not aware of the provisioned throughput it is easyer that you will face errors. Also you cannot use conditions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top