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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南