I have written below Json for insert in dynamodb, but unable to insert. But if I try single element without array it's working fine.
$item = $marshaler->marshalJson('
[
{
"PK":"CATG",
"SK":"NAME#SMART_PHONE",
"TYPE":"CATG",
"ATTR":{
"name":"安いスマートフォン一覧【最安値比較】"
}
},
{
"PK":"CATG#SMART_PHONE",
"SK":"SUBCATG#IPHONE",
"TYPE":"SUBCATG",
"ATTR":{
"name":"IPHONE",
"Compa":"mac",
"uid":123
}
},
{
"PK":"CATG#SMART_PHONE#SUBCATG#IPHONE",
"SK":"PROD#PHONE11",
"TYPE":"PROD",
"ATTR":{
"name":"PHONE11",
"uid":123,
"price":112.02,
"images":[
],
"total":120
}
},
{
"PK":"CATG#SMART_PHONE#SUBCATG#IPHONE",
"SK":"PROD#PHONE7",
"TYPE":"PROD",
"ATTR":{
"name":"PHONE7",
"Compa":"mac",
"uid":124,
"price":102.02,
"images":[
],
"total":80
}
}
]
');
$params = [
'TableName' => $tableName,
'Item' => $item
];
$result = $dynamodb->putItem($params);
After apply this code. I have got below error
Fatal error: Uncaught InvalidArgumentException: The JSON document must be valid and be an object at its root. in E:\xampp\htdocs\shop\vendor\aws\aws-sdk-php\src\DynamoDb\Marshaler.php:99 Stack trace: #0 E:\xampp\htdocs\shop\item.php(30): Aws\DynamoDb\Marshaler->marshalJson('\r\n [\r\n ...') #1 {main} thrown in E:\xampp\htdocs\shop\vendor\aws\aws-sdk-php\src\DynamoDb\Marshaler.php on line 99
If I insert single row it's working fine. How can I insert bulk row ?
To insert multiple items you have two options:
PutItem
API for each oneBatchWriteItem
API (see docs)Note that the BatchWriteItem API will still result in 25 WCU being used but it just saves you a bit on the network round-trips, making fewer calls.
The downside of using BatchWriteItem
is that you have to manage the grouping of items and then handle the partial successful writes, such that unsuccessful items get re-grouped/re-tried.