Posting to an old question because I did not think the solution was easy to find. Hope this helps someone.
Step 1: Create a complex Java object that mirrors the structure desired.
List<HashMap<String, Integer>> cartItems = new ArrayList<HashMap<String, Integer>>();
HashMap<String, Integer> item1 = new HashMap<String, Integer>();
item1.put("ProductId", 100);
item1.put("Quantity", 50);
cartItems.add(item1);
HashMap<String, Integer> item2 = new HashMap<String, Integer>();
item2.put("ProductId", 121);
item2.put("Quantity", 51);
cartItems.add(item2);
Step 2: Update DynamoDB item with the complex object.
I use a helper method:
private void updateAttribute(int id, String newAttribute, Object newValue){
Map<String, Object> newValues = new HashMap<String, Object>();
newValues.put(":value", newValue);
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("id", id)
.withUpdateExpression("set " + newAttribute + " = :value")
.withValueMap(newValues);
siteTable.updateItem(updateItemSpec);
}
and then call thus:
updateAttribute(123, "CartItems", cartItems);
The newly added cart items attribute displays in DynamoDB like:
{
"CartItems": [
{
"ProductId": 100,
"Quantity": 50
},
{
"ProductId": 121,
"Quantity": 51
}
]
}
I have not tested an upsert scenario. In the past, upsert functionality did not seem to be present: https://forums.aws.amazon.com/thread.jspa?threadID=162907
Regarding reads of deeply nested items: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.AccessingItemAttributes.html#DocumentPaths