如何访问来自 AWS Kinesis Data Stream 事件的数据?

How do I access the data from an AWS Kinesis Data Stream event?

我正在开发一个消耗 AWS Kinesis Data Stream 的 Python lambda。但我正在努力了解运动记录事件的形状。例如:

{
    "Records": [
        {
            "kinesis": {
                "kinesisSchemaVersion": "1.0",
                "partitionKey": "1",
                "sequenceNumber": "49590338271490256608559692538361571095921575989136588898",
                "data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
                "approximateArrivalTimestamp": 1545084650.987
            },
            "eventSource": "aws:kinesis",
            "eventVersion": "1.0",
            "eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898",
            "eventName": "aws:kinesis:record",
            "invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role",
            "awsRegion": "us-east-2",
            "eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream"
        },
        {
            "kinesis": {
                "kinesisSchemaVersion": "1.0",
                "partitionKey": "1",
                "sequenceNumber": "49590338271490256608559692540925702759324208523137515618",
                "data": "VGhpcyBpcyBvbmx5IGEgdGVzdC4=",
                "approximateArrivalTimestamp": 1545084711.166
            },
            "eventSource": "aws:kinesis",
            "eventVersion": "1.0",
            "eventID": "shardId-000000000006:49590338271490256608559692540925702759324208523137515618",
            "eventName": "aws:kinesis:record",
            "invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role",
            "awsRegion": "us-east-2",
            "eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream"
        }
    ]
}

来源: Using AWS Lambda with Amazon Kinesis

我最初放在该对象表示的运动流中的数据在哪里?我如何访问这些数据?

您放入流中的数据在每条记录的 kinesis.data 键上表示为 Base64 编码字符串。例如(截断):

{
    "Records": [
        {
            "kinesis": {
                ...
                "data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
                ...
            },
            ...
        },
        {
            "kinesis": {
                ...
                "data": "VGhpcyBpcyBvbmx5IGEgdGVzdC4=",
                ...
            },
            ...
        }
    ]
}

要访问数据,循环遍历每个 Records 对象并使用 Base64 解码 kinesis.data 值。

import base64


for record in event["Records"]:
    decoded_data = base64.b64decode(record["kinesis"]["data"]).decode("utf-8")

    print(decoded_data)
    # Record 1: Hello, this is a test.
    # Record 2: This is only a test.

注意:此示例假设发送到运动流的数据最初是 utf-8 编码,然后运动 b64 对其进行编码。