AWS Lambda DynamoDB查询错误
问题描述:
我正在使用python编写AWS Lambda函数。我正在使用boto3连接到DynamoDB数据库,并试图检索最后一个位置条目。AWS Lambda DynamoDB查询错误
DynamoDB表的主分区键是“Serial”,它是一个字符串,主分类键是“Time”,也是一个字符串。
当我运行这段代码,试图获得序列“0001”,我得到以下错误
def getPriorGeofence(device):
client = boto3.client('dynamodb')
response = client.query(
TableName='Locations',
IndexName='Serial',
Select='ALL_ATTRIBUTES',
Limit=1,
ScanIndexForward=True,
KeyConditions={
"Serial":{
'ComparisonOperator': "EQ",
'AttributeValueList': [device]
}
}
)
return response
错误
{
"stackTrace": [
[
"/var/task/lambda_function.py",
154,
"lambda_handler",
"priorGeofence = getPriorGeofence(serial)"
],
[
"/var/task/lambda_function.py",
110,
"getPriorGeofence",
"'AttributeValueList': [device]"
],
[
"/var/runtime/botocore/client.py",
253,
"_api_call",
"return self._make_api_call(operation_name, kwargs)"
],
[
"/var/runtime/botocore/client.py",
517,
"_make_api_call",
"api_params, operation_model, context=request_context)"
],
[
"/var/runtime/botocore/client.py",
572,
"_convert_to_request_dict",
"api_params, operation_model)"
],
[
"/var/runtime/botocore/validate.py",
270,
"serialize_to_request",
"raise ParamValidationError(report=report.generate_report())"
]
],
"errorType": "ParamValidationError",
"errorMessage": "Parameter validation failed:\nInvalid type for parameter KeyConditions.Serial.AttributeValueList[0], value: 0001, type: <type 'unicode'>, valid types: <type 'dict'>"
}
答
更改AttributeValueList下面所提到的值中的最后一项(即将数据类型明确提及为字符串'S'): -
KeyConditions={
"Serial":{
'ComparisonOperator': "EQ",
'AttributeValueList': [{'S' : device}]
}
}