-
Notifications
You must be signed in to change notification settings - Fork 926
Description
Item collections are a core concept in DynamoDB, especially for "well-designed" applications using a single table design. So ideally, this concept should have first class support in the SDK. However, there doesn't seem to be a way to query a heterogeneous item collection with the Enhanced Client. I did see a feature request (#1870) for supporting polymorphic types, but this doesn't seem to have item collections in mind.
Here's an example of a heterogeneous item collection, a customer and their associated orders:
PK | SK | Type |
---|---|---|
CUSTOMER#Customer1 | A | Customer |
CUSTOMER#Customer1 | ORDER#Order1 | Order |
CUSTOMER#Customer1 | ORDER#Order2 | Order |
To retrieve the item collection we use a Query
with KeyConditionExpression: "PK = CUSTOMER#Customer1"
. Now suppose our application has a value class (ideally immutable) corresponding to each of these item types: Customer.java
and Order.java
. But with the Enhanced Client, there doesn't seem to be a way to marshall heterogeneous query results into their respective value classes.
With the AWS SDK for Java v1 and DynamoDBMapper, I've been doing the following:
- Use
AmazonDynamoDB#query
to query an item collection, e.g.KeyConditionExpression: "PK = CUSTOMER#Customer1"
QueryResult#getItems
returns aList<Map<String, AttributeValue>>
- For each item, check the
Type
attribute and useDynamoDBMapper#marshallIntoObject
to marshall the item'sMap<String, AttributeValue>
into the appropriate value class, e.g.dynamoDBMapper.marshallIntoObject(Customer.class, currentItemKeyAttributeValueMap)
...but I don't think we can do something similar when using the Enhanced Client since the DynamoDBMapper wouldn't work with the AWS SDK v2 annotations and the Enhanced Client doesn't expose such a marshallIntoObject
method. I think working with immutable value classes would make it more challenging to come up with a similar workaround for the Enhanced Client.
I did experiment with the Flat map attributes from another class features in the Enhanced Client, but this doesn't help with the issue of marshalling heterogeneous results into their respective value classes.