[英]How do I access the data from an AWS Kinesis Data Stream event?
I'm working on a Python lambda that consumes an AWS Kinesis Data Stream .我正在研究使用AWS Kinesis Data Stream的 Python lambda。 But I'm struggling to understand the shape of kinesis record events.
但是我很难理解运动记录事件的形状。 For example:
例如:
{
"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"
}
]
}
Source: Using AWS Lambda with Amazon Kinesis来源: 将 AWS Lambda 与 Amazon Kinesis 结合使用
Where is the data I originally put on the kinesis stream represented in this object?我最初放在这个对象中表示的运动流上的数据在哪里? And how do I access this data?
我如何访问这些数据?
The data you put on the stream is represented as a Base64 encoded string on each record's kinesis.data
key.您放入流中的数据表示为每个记录的
kinesis.data
键上的 Base64 编码字符串。 For example (truncated):例如(截断):
{
"Records": [
{
"kinesis": {
...
"data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
...
},
...
},
{
"kinesis": {
...
"data": "VGhpcyBpcyBvbmx5IGEgdGVzdC4=",
...
},
...
}
]
}
To access the data, loop through each Records
object and Base64 decode the kinesis.data
value.要访问数据,请遍历每个
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.
Note: This example assumes that the data sent to the kinesis stream was originally utf-8
encoded before kinesis b64 encoded it.注意:此示例假设发送到 kinesis 流的数据最初是
utf-8
编码,然后 kinesis b64 对其进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.