[英]DynamoDB Boto3 query where FilterExpression attribute name contains a dot
我正在尝试使用 FilterExpression 使用 boto3 查询来查询我的 dynamodb 表,但没有返回任何结果,因为我希望过滤的属性名称具有“。” 在里面。 ExpressionAttributeNames 似乎不适用于 boto3.dynamodb.conditions.Attr,而且我在网上找不到任何类似场景的示例 python 代码。
kwargs = {'IndexName': constants.RESOURCE_TYPE_INDEX, 'KeyConditionExpression': Key(constants.RESOURCE_TYPE).eq(constants.AUTOSCALING_GROUP),
'FilterExpression': Attr('configuration.loadBalancerNames').contains(filter_value),
'ProjectionExpression': 'relationships, PRIMARY_KEY'}
print("Getting associations for: " + constants.AUTOSCALING_GROUP)
retry = True
while retry:
retry = False
try:
response = table.query(**kwargs)
except ClientError as e:
if e.response['Error']['Code'] == 'ProvisionedThroughputExceededException':
time.sleep(5)
retry = True
print("Retrying query")
else:
exception_type = str(e.__class__.__name__)
logger.error(
"Function: utilities.get_asg_records_for_lb_enrichment " + "Exception Type: " + exception_type + " Exception Message:" + str(e))
except Exception as e:
exception_type = str(e.__class__.__name__)
logger.error(
"Function: utilities.get_asg_records_for_lb_enrichment " + "Exception Type: " + exception_type + " Exception Message:" + str(e))
if response:
associations.extend(response.get('Items'))
if response.get('LastEvaluatedKey'):
kwargs['ExclusiveStartKey'] = response.get('LastEvaluatedKey')
retry = True
print("Last evaluated key found: " + str(response.get('LastEvaluatedKey')))
我猜.
在 DynamoDB 中被视为特殊字符,因此请改用表达式属性名称。
所以你的kwargs
看起来像这样,
kwargs = {
'IndexName': constants.RESOURCE_TYPE_INDEX,
'KeyConditionExpression': Key(constants.RESOURCE_TYPE).eq(constants.AUTOSCALING_GROUP),
'FilterExpression': 'contains(#cl, :val)',
'ExpressionAttributeNames': {
'#cl': 'configuration.loadBalancerNames'
},
'ExpressionAttributeValues': {
':val': filter_value
},
'ProjectionExpression': 'relationships, PRIMARY_KEY'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.