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

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ