我正在尝试使用 Python Lambda 函数概括 DynamoDB 查询。有许多 ProjectionExpression 的例子被设置为包括数据库中属性的实际值,例如。 G。 ProjectionExpression='猫,狗'。我找不到任何在变量中设置属性的示例,如下所示。我想传入属性的值以使用查询字符串事件检索。
attr1 = 'cats'
attr2 = 'dogs'
response = table.query(
IndexName='pets-index',
KeyConditionExpression=Key(queryparamKey).eq(queryparamKeyID),
# Get two attr
# Does not work
#ProjectionExpression="attr1, attr2"
# Neither does this
#ExpressionAttributeNames='{"#attr1":attr1,"#attr2":attr2}'
)
This是想法,但适用于 javascript 并使用 ExpressionAttributeNames。
这不可能那么深奥,但我现在才接触Python,我还没有找到任何帖子。请指教。
请您参考如下方法:
如果你只想从你的表中获取 attr1 和 attr2 使用 pets-index 其中 pet_type 分区键是 cats,这应该有效:
response = table.query(
IndexName='pets-index',
KeyConditionExpression='pet_type = :value',
ExpressionAttributeValues={
':value': {'S': 'cats'}
},
ProjectionExpression='attr1,attr2'
)
