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

2024-05-10php开发问题
5

本文介绍了如何在不指定主键的情况下从 DynamoDB 表中获取所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个名为 products 的表,主键为 Id.我想选择表中的所有项目.这是我正在使用的代码:

I have a table called products with primary key Id. I want to select all items in the table. This is the code is I'm using:

$batch_get_response = $dynamodb->batch_get_item(array(
    'RequestItems' => array(

        'products' => array(
            'Keys' => array(
                array( // Key #1
                    'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '1'),
                    'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),
                ),
                array( // Key #2
                    'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '2'),
                    'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),
                ),
            )
        )
    )   
));

是否可以在不指定主键的情况下选择所有项目?我正在使用适用于 PHP 的 AWS 开发工具包.

Is it possible to select all items without specifying the primary key? I'm using the AWS SDK for PHP.

推荐答案

Amazon DynamoDB 提供 <为此目的的 href="http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_Scan.html" rel="noreferrer">Scan 操作,返回一个或多个项目,并且它的属性通过执行一个表的完整扫描.请注意以下两个限制:

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 Low-Level 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>";
    }
    

    这篇关于如何在不指定主键的情况下从 DynamoDB 表中获取所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

    相关推荐

    PHP实现DeepL翻译API调用
    DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
    2025-08-20 php开发问题
    168

    PHP通过phpspreadsheet导入Excel日期数据处理方法
    PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
    2024-10-23 php开发问题
    287

    mediatemple - 无法使用 codeigniter 发送电子邮件
    mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
    2024-08-23 php开发问题
    11

    Laravel Gmail 配置错误
    Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
    2024-08-23 php开发问题
    16

    将 PHPMailer 用于 SMTP 的问题
    Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
    2024-08-23 php开发问题
    4

    关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
    Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
    2024-08-23 php开发问题
    17