[英]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.