Vijay Kumar
Vijay Kumar

Reputation: 2707

In DynamoDB how do I append an element to a list field using Java

I have a record in DynamoDB with an field named imageData, this field is a list of items, each item is a dictionary (map containing key value pairs)

How can I append new items to the list in the following record

 { "ssid": "abcd", "imageData": []}

I tried

Table table = dynamoDB.getTable("my_table");

HashMap imageData = new HashMap();
    imageData.put("imageKey", "xyz.jpg" );
    imageData.put("imageUploadTimestamp", "2016-12-12 23:59:59");

ValueMap map = new ValueMap().withList(":img", Arrays.asList(imageData));

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey(SSID, ssid)
            .withUpdateExpression("set imageData = list_append(imageData, :img )")
            .withValueMap(map);

UpdateItemOutcome result = table.updateItem(updateItemSpec);

Is this the right way? is there a better way?

Upvotes: 2

Views: 3173

Answers (1)

Alexander Patrikalakis
Alexander Patrikalakis

Reputation: 5205

This is a good approach to appending to a list attribute in DynamoDB.

Upvotes: 1

Related Questions