如何在不指定主键的情况下从DynamoDB表中获取所有项目? [英] How can I fetch all items from a DynamoDB table without specifying the primary key?
问题描述
我有一个名为product的表,该表具有主键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">扫描操作,该操作返回一个或多个项目并通过对表格进行全面扫描来获取其属性.请注意以下两个约束:
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.
结果集最终是一致的.
对于性能和消耗的容量单位(即价格)而言,扫描操作的成本可能很高,请参阅
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>";
}
这篇关于如何在不指定主键的情况下从DynamoDB表中获取所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!