Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of maps in a DynamoDB data model

We have a data model with defined properties, but one of the properties allows for dynamic metadata (a list of maps or dictionaries). Using the document model, this property maps fine to a list of Document, however, when I'm having trouble getting this dynamic property to map to anything using DataModel. Is there a way to map dynamic data to documents inside a model class property?

Attempting to map it as a list of dictionaries (which matches the structure of the metadata) fails with the below error:

public List<Dictionary<string, object>> Events { get; set; }

Unable to convert [Amazon.DynamoDBv2.DocumentModel.Document] of type Amazon.DynamoDBv2.DocumentModel.Document to System.Collections.Generic.Dictionary`

Using a type of List<Document> got me the closest, which it now lists out 39 documents, but all the Documents have 0 keys, 0 values.

public List<Document> Events { get; set; }

Ex:

document["Events"].AsListOfDocument().First(); // works, contains the keys and values

datamodel.Events.First(); // does not work, it is an empty document
like image 505
Devon Avatar asked Nov 20 '25 23:11

Devon


1 Answers

I know this is a quite old but hopefully this might help somebody else struggling with this.

Hi Devon,

In case you are trying to create the object to send it to your DynamoDB you can try mapping arbitrary data just as stated in AWS documentation for .NET

Here is the code from the documentation:

try
        {
            DynamoDBContext context = new DynamoDBContext(client);

            // 1. Create a book.
            DimensionType myBookDimensions = new DimensionType()
            {
                Length = 8M,
                Height = 11M,
                Thickness = 0.5M
            };

            Book myBook = new Book
            {
                Id = 501,
                Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data",
                ISBN = "999-9999999999",
                BookAuthors = new List<string> { "Author 1", "Author 2" },
                Dimensions = myBookDimensions
            };

            context.Save(myBook);

Once you have your data in the database you can append a new map to the list with something on the lines of:

var request = new UpdateItemRequest
            {
                TableName = "TableName",
                Key = new Dictionary<string, AttributeValue>() { { "PartitionKey", new AttributeValue { S = "value" } } },
                ExpressionAttributeNames = new Dictionary<string, string>()
                {
                    { "#A", "A" }
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {
                        ":val", new AttributeValue
                        {
                            L = new List<AttributeValue>()
                            {
                                {
                                    new AttributeValue
                                    {
                                        M = new Dictionary<string, AttributeValue>()
                                        {
                                            { "Address", new AttributeValue{S = "Value" } },
                                            { "Latitude", new AttributeValue {  S =  position.Latitude.ToString() } },
                                            { "Longitude", new AttributeValue {  S =  position.Longitude.ToString() } }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                UpdateExpression = "SET #A = list_append(#A, :val)"
            };

            try
            {
                var response = await client.UpdateItemAsync(request);
                
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

This did the trick for me, I hope it does for someone else.

. . .

PS: BTW this is my first answer in Stackoverflow and it feels nice to try to contribute when I have came here multiple times for answers that saved me time jajajaja

like image 178
Gelo Avatar answered Nov 22 '25 15:11

Gelo