我编写了以下 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文档 https://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Table.query:
单个Query
操作将读取最大数量的项目集(如果使用Limit
参数)或最多 1 MB 的数据,然后使用以下方法对结果应用任何过滤FilterExpression
. If LastEvaluatedKey
出现在响应中,您将需要对结果集进行分页。有关更多信息,请参阅对结果进行分页 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Pagination在 Amazon DynamoDB 开发人员指南 中。
这其实是没有的boto3
- 限制,但底层的限制query
-API.
您可以使用boto3
's 内置分页 https://boto3.readthedocs.io/en/latest/guide/paginators.html。这是一个显示使用的示例用于查询 DynamoDB 表的分页器 https://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Paginator.Query由...提供boto3
:
import boto3
from boto3.dynamodb.conditions import Key
dynamodb_client = boto3.client('dynamodb')
paginator = dynamodb_client.get_paginator('query')
page_iterator = paginator.paginate(
TableName='XYZ',
KeyConditionExpression='mplaceId = :mplaceId',
ExpressionAttributeValues={':mplaceId': {'S' : mplaceid}}
)
for page in page_iterator:
print(page['Items'])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)