question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DynamoDB Enhanced Client: Support Querying and Marshalling Heterogeneous Item Collections

See original GitHub issue

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 (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:

  1. Use AmazonDynamoDB#query to query an item collection, e.g. KeyConditionExpression: "PK = CUSTOMER#Customer1"
  2. QueryResult#getItems returns a List<Map<String, AttributeValue>>
  3. For each item, check the Type attribute and use DynamoDBMapper#marshallIntoObject to marshall the item’s Map<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:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
helloworldlesscommented, Dec 8, 2020

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!

0reactions
github-actions[bot]commented, Mar 27, 2021

⚠️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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Heterogeneous Item Collections in the ...
In this post, I demonstrate a strategy for querying heterogeneous item collections with the AWS SDK v2 for Java's Enhanced Client.
Read more >
Using the DynamoDB Enhanced Client in the AWS SDK for ...
The DynamoDB enhanced client is a high-level library that is part of the AWS SDK for Java version 2 (v2). It offers a...
Read more >
Querying an Item Collection of Distinct Types Java
Example: an item collection which contains a Customer, an Order, ... Java and Enhanced Client: they don't support querying heterogeneous item collections.
Read more >
The What, Why, and When of Single-Table Design with ...
To follow these pointers, the SQL language for querying relational ... An item collection in DynamoDB refers to all the items in a...
Read more >
Load, Save, Query, Delete, with DynamoDB Mapper - YouTube
In this video, I show you how to perform load, save, query and delete operations using DynamoDB Mapper in Java.Become a Better Developer ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found