php 如何在不指定主键的情况下从 DynamoDB 表中获取所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10450962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I fetch all items from a DynamoDB table without specifying the primary key?
提问by Warrior
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:
我有一个名为 products 的表,带有主键Id。我想选择表中的所有项目。这是我正在使用的代码:
$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),
),
)
)
)
));
Is it possible to select all items without specifying the primary key? I'm using the AWS SDK for PHP.
是否可以在不指定主键的情况下选择所有项目?我正在使用适用于 PHP 的 AWS 开发工具包。
回答by Steffen Opel
Amazon DynamoDBprovides the Scanoperation 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:
Amazon DynamoDB 为此提供了Scan操作,它通过对表执行完整扫描来返回一个或多个项目及其属性。请注意以下两个限制:
Depending on your table size, you may need to use pagination to retrieve the entire result set:
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 result set is eventually consistent.
The Scan operation is potentially costly regarding both performance and consumed capacity units (i.e. price), see section Scan and Query Performancein Query and Scan in 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]
根据您的表大小,您可能需要使用分页来检索整个结果集:
注意
如果扫描的项目总数超过 1MB 限制,则扫描停止,结果将返回给用户,并带有 LastEvaluatedKey 以在后续操作中继续扫描。结果还包括超出限制的项目数。扫描可能导致没有满足过滤条件的表数据。结果集最终是一致的。
Scan 操作在性能和消耗的容量单位(即价格)方面可能代价高昂,请参阅Amazon DynamoDB中查询和扫描中的扫描和查询性能部分:
[...] 此外,随着表的增长,扫描操作会变慢。扫描操作会检查每个项目的请求值,并且可以在单个操作中用尽大表的预配置吞吐量。为了加快响应时间,请改用可使用 Query、Get 或 BatchGetItem API 的方式设计表。或者,将您的应用程序设计为以最小化对表请求率影响的方式使用扫描操作。有关更多信息,请参阅Amazon DynamoDB 中的预配置吞吐量指南。[强调我的]
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:
您可以在使用 AWS SDK for PHP Low-Level API for Amazon DynamoDB 扫描表中找到有关此操作的更多详细信息和一些示例片段,其中最简单的示例说明了该操作:
$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>";
}
回答by StefaDesign
I figured you are using PHP but not mentioned (edited). I found this question by searching internet and since I got solution working , for those who use nodejs here is a simple solution using scan :
我认为您正在使用 PHP,但未提及(已编辑)。我通过搜索互联网发现了这个问题,因为我得到了解决方案,对于那些使用 nodejs 的人来说,这里是一个使用 scan 的简单解决方案:
var dynamoClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: config.dynamoClient.tableName, // give it your table name
Select: "ALL_ATTRIBUTES"
};
dynamoClient.scan(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});
I assume same code can be translated to PHP too using different AWS SDK
我假设使用不同的 AWS SDK 也可以将相同的代码转换为 PHP
回答by Chiranjeevi Medicharla
Hi you can download using boto3. In python
您好,您可以使用 boto3 下载。在蟒蛇中
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Table')
response = table.scan()
items = response['Items']
while 'LastEvaluatedKey' in response:
print(response['LastEvaluatedKey'])
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
items.extend(response['Items'])
回答by Hassan Raza
I fetch all items from dynamodb with the following query. It works fine. i create these function generic in zend framework and access these functions over the project.
我使用以下查询从 dynamodb 中获取所有项目。它工作正常。我在 zend 框架中创建这些通用函数并通过项目访问这些函数。
public function getQuerydata($tablename, $filterKey, $filterValue){
return $this->getQuerydataWithOp($tablename, $filterKey, $filterValue, 'EQ');
}
public function getQuerydataWithOp($tablename, $filterKey, $filterValue, $compOperator){
$result = $this->getClientdb()->query(array(
'TableName' => $tablename,
'IndexName' => $filterKey,
'Select' => 'ALL_ATTRIBUTES',
'KeyConditions' => array(
$filterKey => array(
'AttributeValueList' => array(
array('S' => $filterValue)
),
'ComparisonOperator' => $compOperator
)
)
));
return $result['Items'];
}
//Below i Access these functions and get data.
$accountsimg = $this->getQuerydataWithPrimary('accounts', 'accountID',$msgdata[0]['accountID']['S']);
回答by Dushyant Singh Chouhan
This C# code is to fetch all items from a dynamodb table using BatchGet or CreateBatchGet
此 C# 代码是使用 BatchGet 或 CreateBatchGet 从 dynamodb 表中获取所有项目
string tablename = "AnyTableName"; //table whose data you want to fetch
var BatchRead = ABCContext.Context.CreateBatchGet<ABCTable>(
new DynamoDBOperationConfig
{
OverrideTableName = tablename;
});
foreach(string Id in IdList) // in case you are taking string from input
{
Guid objGuid = Guid.Parse(Id); //parsing string to guid
BatchRead.AddKey(objGuid);
}
await BatchRead.ExecuteAsync();
var result = BatchRead.Results;
// ABCTable is the table modal which is used to create in dynamodb & data you want to fetch
// ABCTable 是表格模式,用于在 dynamodb 中创建您要获取的数据

