简体   繁体   中英

Can not send a Kinesis data stream to DynamoDB

I'm having a problem sending a data stream from kinesis to DynamoDB. I used this lambda function to do the job. When I try the test button in the lambda, it sends the data to DynamoDB. However, when I try to send from the data stream, I get this error "string indices must be integers". Any solution please? Thanx

Here is the code

import boto3
import datetime
import base64
import json

def lambda_handler(event, context):
    try:
        dynamo_db = boto3.resource('dynamodb')
        table = dynamo_db.Table('detected-faces')
        for record in event["Records"]:
            encoded = record["kinesis"]["data"]
            decoded = json.loads(base64.b64decode(encoded).decode("utf-8"))
            for detectedFaces in decoded:
                matchedFaces = detectedFaces["MatchedFaces"]
                for face in matchedFaces:
                    data = {
                        'FaceId': face["Face"]["FaceId"],
                        'Confidence': str(face["Face"]["Confidence"]),
                        'Similarity': str(face["Similarity"]),
                        'ExternalImageId':face["Face"]["ExternalImageId"],
                        'ImageId':face["Face"]["ImageId"]
                    }
                    # table.put_item(Item=data)  
                    with table.batch_writer() as batch_writer:
                        batch_writer.put_item(Item=data)
    except Exception as e: 
        print(str(e))

here is the lambda test

{
  "Records": [
    {
      "kinesis": {
        "kinesisSchemaVersion": "1.0",
        "partitionKey": "4522972768",
        "sequenceNumber": "49616873388568555882989759900514551623368944523335958530",
        "data": "W3siRGV0ZWN0ZWRGYMDYyMzUzNS1kYWY4LTQ5MjgtYmM2ZS1hOTk0MzliMTIzYmEiLCJDb25maWRlbmNlIjo5OS45NjczLCJJbWFnZUlkIjoiYjhjMDYyNWItOGEwMS0zNGJmLWJkZGMtMGM4YmI5NGMyMGFmIiwiRXh0ZXJuYWxJbWFnZUlkIjoiYWxhYSJ9fV19XQo=",
        "approximateArrivalTimestamp": 1617193796.445
      },
      "eventSource": "aws:kinesis",
      "eventVersion": "1.0",
      "eventID": "shardId-000000000000:496168733885685558829897599005148787423368944523335958530",
      "eventName": "aws:kinesis:record",
      "invokeIdentityArn": "arn:aws:iam::731558896689:role/lambda-kinesis-dynamodb",
      "awsRegion": "eu-west-1",
      "eventSourceARN": "arn:aws:kinesis:eu-west-1:731558896689:stream/dynmoDB-kds-stream"
    }
  ]
}

Here is the return of the data stream:

START RequestId: 1f40c1db-7f67-4fd2-8f2e-637c94ab08b8 Version: $LATEST
string indices must be integers
END RequestId: 1f40c1db-7f67-4fd2-8f2e-637c94ab08b8
REPORT RequestId: 1f40c1db-7f67-4fd2-8f2e-637c94ab08b8  Duration: 56.43 ms  Billed Duration: 57 ms  Memory Size: 128 MB Max Memory Used: 72 MB  
START RequestId: 0742e744-1b85-4169-bdcf-b6e0f902d9c4 Version: $LATEST
string indices must be integers
END RequestId: 0742e744-1b85-4169-bdcf-b6e0f902d9c4
REPORT RequestId: 0742e744-1b85-4169-bdcf-b6e0f902d9c4  Duration: 48.96 ms  Billed Duration: 49 ms  Memory Size: 128 MB Max Memory Used: 72 MB  
START RequestId: c3414096-d77d-4db2-ab1f-7ab48c58c627 Version: $LATEST
string indices must be integers
END RequestId: c3414096-d77d-4db2-ab1f-7ab48c58c627
REPORT RequestId: c3414096-d77d-4db2-ab1f-7ab48c58c627  Duration: 105.43 ms Billed Duration: 106 ms Memory Size: 128 MB Max Memory Used: 72 MB  

SOLVED The for loop should have been like this

for record in event["Records"]:
            encoded = record["kinesis"]["data"]
            decoded = json.loads(base64.b64decode(encoded).decode("utf-8"))
            for faceSearchResponse in decoded["FaceSearchResponse"]:
                for matchedFaces in faceSearchResponse["MatchedFaces"]:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM