2017-02-21 206 views
1

我正在使用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'>" 
} 

回答

0

更改AttributeValueList下面所提到的值中的最後一項(即將數據類型明確提及爲字符串'S'): -

KeyConditions={ 
     "Serial":{ 
      'ComparisonOperator': "EQ", 
      'AttributeValueList': [{'S' : device}] 
     } 
    }