从 DynamoDb 查询的 Python 脚本不提供所有项目

hat*_*lla 4 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']))
Run Code Online (Sandbox Code Playgroud)

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

它发生的任何原因?

Dun*_*dan 8

dynamodb.Table.query() returns at max 1MB of data. From the 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 .

That's actually no boto3-limitation, but a limitation of the underlying query-API.

Instead of implementing pagination yourself, you can use boto3's built-in pagination . Here is an example showing the use of the paginator for querying DynamoDB tables provided by 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'])
Run Code Online (Sandbox Code Playgroud)