【发布时间】:2019-01-11 19:26:09
【问题描述】:
我已经编写了以下 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 API 仅返回 1 MB 数据。如果有更多数据,DDB 会对其进行分页。如果响应中存在 LastEvaluatedKey,则需要对结果集进行分页。文档可以在这里找到:boto3.readthedocs.io/en/latest/reference/services/…
标签: amazon-web-services amazon-dynamodb