我已经编写了以下 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 theLimit
parameter) or a maximum of 1 MB of data and then apply any filtering to the results usingFilterExpression
. IfLastEvaluatedKey
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/