DynamoDB Enhanced Client: Support Querying and Marshalling Heterogeneous Item Collections
See original GitHub issueItem 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 (https://github.com/aws/aws-sdk-java-v2/issues/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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:6 (2 by maintainers)
After digging into the Enhanced Client code, I discovered this,
software.amazon.awssdk.enhanced.dynamodb.TableSchema#mapToItem
, which is exactly what I was after!Used together with
software.amazon.awssdk.services.dynamodb.DynamoDbClient#query
, I’m able to query an item collection and then marshall each item into its respective entity.I’ve described how to do this in detail here.
I’m fine with closing this!
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.