amazon-web-services - 从 DynamoDb 查询的 Python 脚本不提供所有项目

标签 amazon-web-services amazon-dynamodb

我已经编写了以下 python 代码来从表中获取数据,但它没有按照我的需要获取所有项目。当我查看 DynamoDb 的 AWS 控制台页面时,与我从脚本中获得的条目相比,我可以看到更多的条目。

from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from datetime import datetime
from boto3.dynamodb.conditions import Key, Attr
import sys

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb', aws_access_key_id = '',
        aws_secret_access_key = '',
        region_name='eu-west-1', endpoint_url="http://dynamodb.eu-west-1.amazonaws.com")

mplaceId = int(sys.argv[1])
table = dynamodb.Table('XYZ')

response = table.query(
    KeyConditionExpression=Key('mplaceId').eq(mplaceId)
)

print('Number of entries found ', len(response['Items']))

我也从 aws 控制台做了同样的事情。通过 mplaceId 查询。

它发生的任何原因?

最佳答案

dynamodb.Table.query()最多返回 1MB 的数据。来自 boto3 documentation :

A single Query operation will read up to the maximum number of items set (if using the Limit parameter) or a maximum of 1 MB of data and then apply any filtering to the results using FilterExpression. If LastEvaluatedKey is present in the response, you will need to paginate the result set. For more information, see Paginating the Results in the Amazon DynamoDB Developer Guide .



那实际上不是boto3 - 限制,但底层的限制 query -API。

您可以使用 boto3 而不是自己实现分页的 built-in pagination .这是一个示例,显示了 paginator for querying DynamoDB tables 的使用由 boto3 提供:

dynamodb_client = boto3.client('dynamodb')
paginator = dynamodb_client.get_paginator('query')
page_iterator = paginator.paginate(KeyConditionExpression=Key('mplaceId').eq(mplaceId))

for page in page_iterator:
    print(page['Items'])

关于amazon-web-services - 从 DynamoDb 查询的 Python 脚本不提供所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51680463/

相关文章:

go - 使用 golang 编码/解码 "enum"到 DynamoDB

amazon-dynamodb - dynamodb 扫描中的多个 FilterExpression

json - 如何在 C# 中为 Amazon Dynamo DB 中的 Json 属性(列值)创建全局二级索引?

amazon-web-services - 撤消 aws s3 策略以拒绝所有用户的所有操作

amazon-web-services - Github 工作流 CI/CD 失败

python - boto3 改变 AWS ec2 实例状态

node.js - AWS Cognito + 我自己的使用 DynamoDB 的身份解决方案?

mysql - 检查给定句子(查询)是否包含任何预定义关键字的最佳方法

amazon-web-services - 如何在 AWS::ElasticBeanstalk::Environment 中设置环境变量?

ruby-on-rails - 从 Rails 应用程序上的 s3 存储桶中获取 1000 多个对象