首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在不指定主键的情况下从DynamoDB表中获取所有项?

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

Stack Overflow用户
提问于 2012-05-04 22:38:22
回答 5查看 141.2K关注 0票数 60

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

代码语言:javascript
复制
$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),
                ),
            )
        )
    )   
));

是否可以在不指定主键的情况下选择所有项目?我使用的是AWS SDK for PHP。

EN

回答 5

Stack Overflow用户

发布于 2020-01-24 21:25:17

您好,您可以使用boto3下载。在python中

代码语言:javascript
复制
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'])
票数 16
EN

Stack Overflow用户

发布于 2015-06-05 20:05:39

我使用以下查询从dynamodb获取所有项。它工作得很好。我在zend框架中创建了这些通用函数,并通过项目访问这些函数。

代码语言:javascript
复制
        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']);
票数 2
EN

Stack Overflow用户

发布于 2020-08-24 11:36:53

这是一个简单的代码,通过指定DynamoDB服务的区域来列出AWS表中的所有项目。

代码语言:javascript
复制
import boto3

dynamodb = boto3.resource('dynamodb', region_name='ap-south-1')
table = dynamodb.Table('puppy_store')
response = table.scan()
items = response['Items']

# Prints All the Items at once
print(items)

# Prints Items line by line
for i, j in enumerate(items):
    print(f"Num: {i} --> {j}")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10450962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档