Search code examples
amazon-web-servicesamazon-dynamodb

Insert unknown attributes in DynamoDB using DynamoDBMapper/DynamoDbTable


I was using DynamoDBMapper to insert data into a dynamoDB table.

For the data I have a corresponding data model annotated with @DynamoDBTable.

Now, there are few attributes that might come from the client side which are not known before hand and can't be modelled. Also these attributes needs to be top-level because I want to have GSIs on all of those.

Is there a way I can model the requirement using DynamoDBMapper or DynamoDbTable(in case of sdk 2.0) ?

I went through @DynamoDBFlattened but it's not working for the use case because it requires me to define all the attributes before hand.

Also there is a ticket for this issue which got closed without any resolution : https://github.com/aws/aws-sdk-java/issues/674


Solution

  • Both SDK clients you reference are for strongly mapping items to class objects. However, your use case requires you to store attributes which are not mapped, which leads me to think you're better off using a different SDK client.

    If the low level client does not suit your use case and you still require some abstraction, then consider using the Document client.

    Interface representing the Document API for DynamoDB. The Document API operations are designed to work with open content, such as data with no fixed schema, data that cannot be modeled using rigid types, or data that has a schema. This interface provides all the methods required to access a Document, as well as constructor methods for creating a Document that can be used to read and write to DynamoDB using the EnhancedDynamoDB client. Additionally, this interface provides flexibility when working with data, as it allows you to work with data that is not necessarily tied to a specific data model.

    https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/document/EnhancedDocument.html