0

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 ?

1 Answer 1

1

To insert multiple items you have two options:

  1. you can have a loop and repeatedly call the PutItem API for each one
  2. you can break up your collection of items into groups of up to 25 and use the BatchWriteItem 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.