I am using AWS SDK for PHP 3.x
A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.
$result = $dynamodbClient->batchWriteItem([
'RequestItems' => [
$tableName => [
[
'PutRequest' => [
'Item' => [
'Id' => ['N' => '1'],
'AlbumTitle' => [
'S' => 'Somewhat Famous',
],
'Artist' => [
'S' => 'No One You Know',
],
'SongTitle' => [
'S' => 'Call Me Today',
],
],
],
],
],
],
]);
For single item its working fine. How can I write more than 25 items.
To write more than 25 items, you have to repeatedly call BatchWriteItem, adding items from your collection, 25 at a time.
Something along these lines (pseudo-code):
requests = []; // use an array to stage your put item requests
foreach(item in SourceCollection) {
addItem(item, requests); // add this item to the array
if(count(requests) == 25) { // when you have 25 ready..
// result = dynamodbClient->batchWriteItem(...)
requests = []; // clean up the array of put item requests
// handle the failed items from the result object
}
}
// handle any remaining requests
while (len(requests) > 0) {
// result = dynamodbClient->batchWriteItem(...)
requests = []
// handle any failed requests, either by re-adding them
// back into the requests array, or doing something else with them
// NOTE: if retrying, need to decide when to give up
}
Make sure to handle failed items from each batchWriteItem result by re-adding them back to the requests