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")
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.
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.