我正在尝试使用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')))发布于 2020-08-06 05:15:45
我猜.在DynamoDB中被认为是一个特殊字符,所以请使用Expression Attribute Name。
所以你的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'
}https://stackoverflow.com/questions/63271808
复制相似问题