Enhanced DynamoDB Client — Java Abstraction Code
I have created a Helper code, which takes a java object and seamlessly executes the transaction onto DynamoDb.
Join the DZone community and get the full member experience.Join For Free
DynamoDb has introduced an enhanced client in 2020 that comes bundled with AWS SDK 2.0. This client is now the suggested way forward to execute database operations on DynamoDB using application classes.
In my recent project, we have had few scenarios to build against —
Execute Insert + Update as set in a transaction. The transactions should fail for the inserts against which the primary key (Partition & Sort) already exists. Also, updates should fail if the record is updated by another process.
Duplicate inserts can be controlled through condition expression in the statements. This is a repeated pattern. There is a need to abstract this out such that not all developers need to delve deeper into dynamo code.
I have created a Helper code, which takes a java object and seamlessly executes the transaction onto DynamoDb. The helper code is available in this github repo. The readme on the repo has greater detail.
There are 3 custom annotations in addition to Dynamodb enhance client annotations that you have to apply on Java POJO.
@Table(name = “ACCOUNT”) — This is where you specify the DynamoDB table in which you post objects.
@PartitionKey — This is a field level annotation. You mark out the partition key relevant to the item
@SortKey — This is a field level annotation. You mark out the sort key of the item
Once POJO is annotated, the developer needs to create a transaction packet and just execute the transaction.
The optimistic handling of updates through versioning of items, using @DynamoDbVersionAttribute is already provided by Enhanced client.
2. Query Dynamo with a variety of sortkey access patterns. The helper class includes methods, which allow developers to use java objects abstraction to query dynamo tables.
The above three annotations of @Table, @ParitionKey, and @SortKey need to be applied. Example
* Fetch a single item given a partition key and sort key
* Fetch a list of items for a given partition key and greater than the provided sort key
The helper also supports condition expressions. The value attribute maps are automatically created through a mix of Java Reflection and Schema support libraries provided by Enhanced Client.
This helper will evolve over time. Currently, @PartitionKey support is limited to Integer and String classes. I hope this library will save time and effort and increase speed on the project.
Opinions expressed by DZone contributors are their own.