1

We have a DynamoDB table Test which has an attribute Template. Bellow are the class definitions. I would like to update the Template attribute or some of its attribute based on certain condition. I tried doing the same using UpdateItemRequest but unable to find a way to update the template attribute since everything is converted to either string, number or bytes.

Code for reference.

@DynamoDbBean
public class Test implements NoSQLEntity {
        
private String name;
private Template template;
        
@DynamoDbAttribute("name")
public String getName() {
    return name;
}
        
@DynamoDbAttribute("template")
public Template getTemplate() {
     return template;
}   
}

   @DynamoDbBean
   public class Template {

   private String pk;
   private String name;
   private List<String> demo;

   @DynamoDbAttribute("pk")
   public String getPk() {
        return this.pk;
   }
            
   @DynamoDbAttribute("name")
   public String getName() {
         return name;
   }

   @DynamoDbAttribute("demo")
   public List<String> getdemo() {
       return demo;
   } 

}

Sample update code:

UpdateItemRequest request = UpdateItemRequest.builder()
            .tableName("Test")
            .key(itemKey)
            .updateExpression("SET tmpt = :tmt")
            .expressionAttributeValues(expressionValues)
            .build();

Here I am unable to build the :tmt using the AttributeValue. Can someone please guide me?

2 Answers 2

1

Here is a Readme from the Eng team that you may find useful:

https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced

Sign up to request clarification or add additional context in comments.

2 Comments

I did go through this but still struggling to find a way to update user defined objects
Thanks for the link !! Converter served my purpose
0

DynamoDb Enhanced provides DefaultAttributeConverterProvider that will identify a converter that can convert the specific Java type into the AttrbuteValue type that can used to update the item.

Smaple Code:

    AttributeValue value = DefaultAttributeConverterProvider.create()
            .converterFor(EnhancedType.documentOf(Template.class, 
             TableSchema.fromBean(Template.class)))
            .transformFrom(template);

Can then put this value in the expressionValues map.

   Map<String, AttributeValue> expressionValues = new HashMap<>();
    expressionValues.put(":tmt", value);

   UpdateItemRequest request = UpdateItemRequest.builder()
        .tableName("Test")
        .key(itemKey)
        .updateExpression("SET tmpt = :tmt")
        .expressionAttributeValues(expressionValues)
        .build();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.