Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

nosql - How to insert multiple row in dynamoDb using PHP?

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:xampphtdocsshopvendorawsaws-sdk-phpsrcDynamoDbMarshaler.php:99 Stack trace: #0 E:xampphtdocsshopitem.php(30): AwsDynamoDbMarshaler->marshalJson('
 [
 ...') #1 {main} thrown in E:xampphtdocsshopvendorawsaws-sdk-phpsrcDynamoDbMarshaler.php on line 99

If I insert single row it's working fine. How can I insert bulk row ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...