问题描述
我有一个名为 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 表中获取所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!