Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Script to query from DynamoDb not giving all items

I have written following python code to fetch data from a table but its not fetching all the items as I want. When I check on AWS console page of DynamoDb, I can see much more entries as compared to what I get from script.

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']))

I did the same thing from aws console also. Query by mplaceId.

Any reason why its happening?

like image 716
hatellla Avatar asked Jan 27 '23 15:01

hatellla


1 Answers

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:

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'])
like image 194
Dunedan Avatar answered Feb 08 '23 15:02

Dunedan