Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the wildcard equivalent for boto3 dynamodb ProjectionExpression

I have a function that calls boto3’s query operation. My data object for each record is rather large so I have created an optional parameter that can be passed to reduce the fields that are returned. Here is the function:

def get_leads(type,leadTable,**kwargs):
    attributes = kwargs.get('attributes', None)
    client = boto3.resource('dynamodb')
    table = client.Table(leadTable)
    try:
        if attributes != None:
            response = table.query(ProjectionExpression=attributes,KeyConditionExpression=Key('lead_type').eq(type))
        else:
            response = table.query(KeyConditionExpression=Key('lead_type').eq(type))
        return response
    except Exception as e:
        print(e)
        return 0

This way, i can optionally pass attributes as a parameter
e.g.,

get_leads("user","table1",attributes="first_name,last_name,title,image_link")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

returns only first_name,last_name,title,image_link fields

OR

get_leads("user","table1")

returns all fields

Ideally, I would like to simplify this function and set attributes to a wildcard ProjectionExpression value when it is not defined.
e.g.,

def get_leads(type,leadTable,**kwargs):
    attributes = kwargs.get('attributes', "*")
    client = boto3.resource('dynamodb')
    table = client.Table(leadTable)
    try:
        return table.query(ProjectionExpression=attributes,KeyConditionExpression=Key('lead_type').eq(type))
    except Exception as e:
        print(e)
        return 0

Sadly, I can’t seem to find documentation anywhere stating what this wildcard value is or if it even exists.

>Solution :

There is no wild card, the absence of a ProjectipnExpression signifies DynamoDB to return all attributes.

Another tip is not to create a client in your function, create it globally and share its state across invocations, you’ll get better latency and reduced costs if you use KMS at scale.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading