C# data type used for Lambda that receives DocumentDB change stream event

0

What is the data type used for a Lambda function that receives a DocumentDB change stream event? For example:

public Task DocumentDbEventConsumer(object document, ILambdaContext context)

What is the correct data type for the "document" parameter?

I'm having trouble finding this information in the documentation and the examples always use Python which does not show/indicate data types. Thanks.

질문됨 5달 전197회 조회
2개 답변
0

Hi whighsmith,

looks like it's a json event.

See here:

https://docs.aws.amazon.com/lambda/latest/dg/with-documentdb.html

{
    "eventSourceArn": "arn:aws:rds:us-east-1:123456789012:cluster:canaryclusterb2a659a2-qo5tcmqkcl03",
    "events": [
        {
            "event": {
                "_id": {
                    "_data": "0163eeb6e7000000090100000009000041e1"
                },
                "clusterTime": {
                    "$timestamp": {
                        "t": 1676588775,
                        "i": 9
                    }
                },
                "documentKey": {
                    "_id": {
                        "$oid": "63eeb6e7d418cd98afb1c1d7"
                    }
                },
                "fullDocument": {
                    "_id": {
                        "$oid": "63eeb6e7d418cd98afb1c1d7"
                    },
                    "anyField": "sampleValue"
                },
                "ns": {
                    "db": "test_database",
                    "coll": "test_collection"
                },
                "operationType": "insert"
            }
        }
    ],
    "eventSource": "aws:docdb"
}

Sincerely Heiko

profile picture
HeikoMR
답변함 5달 전
  • Thank you for your answer; however, there should already be a model type defined to consume this JSON data. Is there not? The internal event data looks like "ChangeStreamDocument<BsonDocument>" which comes from the model defined in MongoDB.Driver. Do I have to generate my own model class from the JSON structure or is there a type pre-defined?

    Generally, the class looks like the following: Note the use of "ChangeStreamDocument<BsonDocument>"

    public partial class Root
    {
        [JsonProperty("eventSourceArn")]
        public string EventSourceArn { get; set; }
    
        [JsonProperty("events")]
        public EventElement[] Events { get; set; }
    
        [JsonProperty("eventSource")]
        public string EventSource { get; set; }
    }
    
    public partial class EventElement
    {
        [JsonProperty("event")]
        public ChangeStreamDocument<BsonDocument> Event { get; set; }
    }
    

    Thanks.

0

I went with this solution. I generated classes from the incoming JSON data, then had to update generated types to use BsonTimestamp for $timestamp and BsonBinaryData for $binary data.

public Task DocumentDbEventConsumer(object events, ILambdaContext context)
{
    var jsonString = events.ToString(); //Get JSON data
    var bsonDocument = BsonSerializer.Deserialize<BsonDocument>(jsonString); //Deserialize to BsonDocument
    var docDbEvents = BsonSerializer.Deserialize<DocumentDbEvents>(bsonDocument); //Deserialize bsonDocument to the class generated from JSON data.

There is likely already a type that can be used instead of "object", but I don't know what it is. If you know, then please post your answer here. Thanks.

답변함 5달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠