且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不指定主键的情况下从DynamoDB表中获取所有项目?

更新时间:2023-09-10 15:24:10

Amazon DynamoDB 提供了扫描操作,该操作返回一个或多个项目并通过对表格进行全面扫描来获取其属性.请注意以下两个约束:

Amazon DynamoDB provides the Scan operation for this purpose, which returns one or more items and its attributes by performing a full scan of a table. Please be aware of the following two constraints:

  • 取决于表的大小,您可能需要使用分页来检索整个结果集:

  • Depending on your table size, you may need to use pagination to retrieve the entire result set:

注意
如果扫描的项目总数超过1MB的限制,则 扫描停止,结果通过以下方式返回给用户: LastEvaluatedKey以在后续操作中继续扫描.这 结果还包括超出限制的项目数.扫描 可能导致表格数据不符合过滤条件.

Note
If the total number of scanned items exceeds the 1MB limit, the scan stops and results are returned to the user with a LastEvaluatedKey to continue the scan in a subsequent operation. The results also include the number of items exceeding the limit. A scan can result in no table data meeting the filter criteria.

结果集最终是一致的.

  • 对于性能和消耗的容量单位(即价格)而言,扫描操作的成本可能很高,请参阅扫描和查询性能部分.com/amazondynamodb/latest/developerguide/QueryAndScan.html"rel =" noreferrer>在Amazon DynamoDB中查询和扫描:

  • The Scan operation is potentially costly regarding both performance and consumed capacity units (i.e. price), see section Scan and Query Performance in Query and Scan in Amazon DynamoDB:

    [...]此外,随着表的增长,扫描操作也会变慢.扫描 操作检查每个项目的请求值,并且可以用完 在单个操作中为大表配置的吞吐量. 为了缩短响应时间,请以一种可以使用的方式设计表 而是Query,Get或BatchGetItem API.或者,设计您的 应用程序以最小化影响的方式使用扫描操作 您桌子上的要求费率.有关更多信息,请参见已配置 Amazon DynamoDB中的吞吐量准则. [强调我的]

    [...] Also, as a table grows, the scan operation slows. The scan operation examines every item for the requested values, and can use up the provisioned throughput for a large table in a single operation. For quicker response times, design your tables in a way that can use the Query, Get, or BatchGetItem APIs, instead. Or, design your application to use scan operations in a way that minimizes the impact on your table's request rate. For more information, see Provisioned Throughput Guidelines in Amazon DynamoDB. [emphasis mine]

  • 您可以在使用适用于Amazon DynamoDB的AWS SDK for PHP低级API ,其中最简单的示例说明了该操作:

    You can find more details about this operation and some example snippets in Scanning Tables Using the AWS SDK for PHP Low-Level API for Amazon DynamoDB, with the most simple example illustrating the operation being:

    $dynamodb = new AmazonDynamoDB();
    
    $scan_response = $dynamodb->scan(array(
        'TableName' => 'ProductCatalog' 
    ));
    
    foreach ($scan_response->body->Items as $item)
    {
        echo "<p><strong>Item Number:</strong>"
             . (string) $item->Id->{AmazonDynamoDB::TYPE_NUMBER};
        echo "<br><strong>Item Name: </strong>"
             . (string) $item->Title->{AmazonDynamoDB::TYPE_STRING} ."</p>";
    }