我试图将数据发送到流,然后使用kinesis firehouse将数据传递到ElasticSearch,我使用python lambda函数将数据转换为JSON,然后再推送,但是lambda失败并出现以下错误。
[ERROR] KeyError: 'Records'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 7, in lambda_handler
for record in event["Records"]:
我可以使用如下所示的sharditerator来查看碎片中的记录。
{
"Records": [
{
"SequenceNumber": "49599580114447666780699883212202628138922281244234350610",
"ApproximateArrivalTimestamp": 1568741427.415,
"Data": "MjAwNi8wMS8wMSAwMDowMDowMHwzMTA4IE9DQ0lERU5UQUwgRFJ8M3wzQyAgICAgICAgfDExMTV8MTA4NTEoQSlWQyBUQUtFIFZFSCBXL08gT1dORVJ8MjQwNHwzOC41NTA0MjA0N3wtMTIxLjM5MTQxNTh8MjAxOS8wOS8xNyAyMzowMDoyNA==",
"PartitionKey": "1"
},
我正在使用下面的lambda函数来处理流。
import json
print("Loading the function")
success = 0
failure = 0
def lambda_handler(event, context):
for record in event["Records"]:
print(record)
payload=base64.b64decode(record["Data"]).decode('utf-8')
match = payload.split('|')
result = {}
if match:
# create a dict object of the row
#build all fields from array
result["crime_time"] = match[0]
result["address"] = match[1]
result['district'] = int(match[2])
result['beat'] = match[3]
result['grid'] =int(match[4])
result['description'] = match[5]
result['crime_id'] = int(match[6])
result['latitude'] = float(match[7])
result['longitude'] = float(match[8])
result['load_time'] = match[9]
result['location'] = {
'lat' : float(match[7]),
'lon' : float(match[8])
}
success+=1
return {
'statusCode': 200,
'body': json.dumps(result)
}
但是在将数据发送到流之后,我在lambda函数中遇到错误。
答案 0 :(得分:0)
什么是错误,您是否检查了Cloudwatch日志以查看发生了什么?我觉得这将为您提供一个很好的指示。