-
Notifications
You must be signed in to change notification settings - Fork 909
DynamoDB Enhanced Client: Support Querying and Marshalling Heterogeneous Item Collections #2151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've been thinking about this a bit more. Below are some workarounds I came up with. I think it's useful to document them because they all have significant drawbacks. 1. Use Lower Level API and Manually Construct the Entities QueryResponse queryResponse = dynamoDbClient.query(queryRequest);
List<Map<String, AttributeValue>> items = queryResponse.items();
// For each item, manually construct the appropriate immutable value class based on the Type attribute
// Omitting some code here... We know this item is an Order because it has an attribute Type with the value "Order"
Order.builder()
.orderId(item.get("OrderId").s())
.orderDate(LocalDateAttributeConverter.create().transformTo(item.get("OrderDate")))
.build(); 2. Make Multiple Round Trips 3. Use a Super Entity and Mappings |
Hi @helloworldless thank you for reaching out, marking as a feature request. It is similar to the polymorphic types request, except within a collection of items instead of an item. |
I think by far the simplest solution here would be to expose the Enhanced Client's marshalling mechanism just like DynamoDBMapper provides |
After digging into the Enhanced Client code, I discovered this, Used together with I've described how to do this in detail here. I'm fine with closing this! |
Glad you are not blocked anymore. Closing this, feel free to reach out if you have any more questions. |
|
…0e497adf1 Pull request: release <- staging/40fbf60b-c3e9-458a-b106-4c80e497adf1
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:
To retrieve the item collection we use a
Query
withKeyConditionExpression: "PK = CUSTOMER#Customer1"
. Now suppose our application has a value class (ideally immutable) corresponding to each of these item types:Customer.java
andOrder.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:
AmazonDynamoDB#query
to query an item collection, e.g.KeyConditionExpression: "PK = CUSTOMER#Customer1"
QueryResult#getItems
returns aList<Map<String, AttributeValue>>
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.
The text was updated successfully, but these errors were encountered: