如何使用batchWriteItem使用PHP将超过25个项目写入DynamoDB表

sam*_*son 7 amazon-dynamodb

我正在使用适用于 PHP 3.x 的 AWS 开发工具包

对 BatchWriteItem 的单个调用最多可以写入 16 MB 的数据,其中可以包含多达 25 个放置或删除请求。要写入的单个项目可以大到 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',
            ],
          ],
        ],
      ],          
    ],
  ],
]);
Run Code Online (Sandbox Code Playgroud)

对于单个项目,它工作正常。我怎么能写超过 25 个项目。

Mik*_*scu 6

要写入超过 25 个项目,您必须重复调用 BatchWriteItem,从您的集合中添加项目,一次 25 个。

沿着这些路线的东西(伪代码):

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
    }
}
Run Code Online (Sandbox Code Playgroud)

确保通过将它们重新添加回请求来处理每个 batchWriteItem 结果中的失败项目