【问题标题】:ValidationException while doing PutItem: Missing the key in the item: ClientError执行 PutItem 时出现 ValidationException:缺少项目中的键:ClientError
【发布时间】:2018-07-10 15:01:40
【问题描述】:

我已经在 Python 2.7 中配置了一个 AWS Lambda,以从 Firehose 传输流中读取事件,并将属性“element_class”(字符串类型)作为分区键写入 DynamoDB 表“My_Tab”

import json
import boto3

def lambda_data_handler(event, context):

    dynamodb = boto3.resource('dynamodb', region_name='ap-south-1')
    table = dynamodb.Table('My_Tab')

    response = table.put_item(Item = event)
    print(json.dumps(response))
    print("Row-" + str(index) + " written to DynamoDB successfully")

对于流式传输到 Firehose,我对 JSON 文件 my_data.json 进行二进制编码,然后使用 AWS CLI put-record 实用程序发送数据,如下所示:

c:\Program Files\Amazon\AWSCLI>aws firehose put-record --delivery-stream-name My_Dlv_Stream --record file://C:/Users/somnath/my_data.json
{
    "RecordId": "DvH2dm5W75F9+bwjJesUW8FoPqQZJOF66etwGoWUycMX..."
}

JSON 文件 my_data.json 有一条 JSON 记录,如下所示:

{"Data":"{\"element_class\":\"1001\"}\n"}

但数据未写入 DynamoDB 表 My_Tab 并显示以下 CloudWatch 错误日志:

An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key element_class in the item: ClientError
Traceback (most recent call last):
File "/var/task/lambda_KFH_2_DynDB.py", line 21, in lambda_data_handler
response = table.put_item(Item = event)
File "/var/task/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/var/task/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/var/task/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/task/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key element_class in the item

【问题讨论】:

  • 在函数开头添加print(json.dumps(event))。您的活动是否有顶级密钥 element_class
  • 是的,我的活动有一个名为“element_class”的顶级键。它将有一个字符串值
  • 您能否从日志中添加print(json.dumps(event)) 的输出?

标签: amazon-web-services aws-lambda amazon-dynamodb amazon-dynamodb-streams


【解决方案1】:

只是添加了下面的代码来从事件的记录属性的recordId中提取数据,而不是直接获取事件。它奏效了。

    it = json.loads(event['records'][0]['data'])
    response = table.put_item(Item = it)

@blueCat :感谢您指出打印事件。我期待的数据格式是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2018-03-14
    • 1970-01-01
    • 2018-01-31
    • 2019-12-05
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多