DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Trending

  • A Walk-Through of the DZone Article Editor
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  • Docker Hardened Images Are Free Now — Here's What You Still Need to Build
  1. DZone
  2. Data Engineering
  3. Databases
  4. Enhanced DynamoDB Client — Java Abstraction Code

Enhanced DynamoDB Client — Java Abstraction Code

I have created a Helper code, which takes a java object and seamlessly executes the transaction onto DynamoDb.

By 
Gaurav Gupta user avatar
Gaurav Gupta
·
Nov. 05, 20 · Analysis
Likes (2)
Comment
Save
Tweet
Share
11.6K Views

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 —

  1. 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.

  1. @Table(name = “ACCOUNT”) — This is where you specify the DynamoDB table in which you post objects.

  2. @PartitionKey — This is a field level annotation. You mark out the partition key relevant to the item

  3. @SortKey — This is a field level annotation. You mark out the sort key of the item

Java
 




xxxxxxxxxx
1
36


 
1
@Data
2
@ToString
3
@Table(name = "ACCOUNT")
4
@DynamoDbBean
5
public class FakeStudent {
6
 
          
7
    // Department
8
    @PartitionKey
9
    private String PARTITION_KEY;
10
 
          
11
    // roll number
12
    @SortKey
13
    private String SORT_KEY;
14
 
          
15
    private Integer fees;
16
    private String ID;
17
    private String STUDENT_NAME;
18
    private FakePicture img;
19
    private Integer version;
20
 
          
21
 
          
22
    @DynamoDbVersionAttribute
23
    public Integer getVersion() {
24
        return version;
25
    }
26
 
          
27
    @DynamoDbPartitionKey
28
    public String getPARTITION_KEY() {
29
        return PARTITION_KEY;
30
    }
31
 
          
32
    @DynamoDbSortKey
33
    public String getSORT_KEY() {
34
        return SORT_KEY;
35
    }
36
}
37
 
          



Once POJO is annotated, the developer needs to create a transaction packet and just execute the transaction.

Java
 




xxxxxxxxxx
1


 
1
 EHelper.TxnPacket packet = EHelper.TxnPacket.builder()
2
   .update(item1, FakeStudent.class, null )
3
   .insert(item2, FakeStudent.class)
4
   .build();
5
 
          
6
EHelper.executeTransactionWrite(packet);



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

Java
 




x





1
FakeStudent s1 = new FakeStudent();
2
// department
3
s1.setPARTITION_KEY("Engineering");
4
// roll number;
5
s1.setSORT_KEY("120");
6
 
          
7
FakeStudent item1 = EHelper.getItem(s1);



* Fetch a list of items for a given partition key and greater than the provided sort key

Java
 




x






1
FakeStudent s1 = new FakeStudent();
2
// department
3
s1.setPARTITION_KEY("Engineering");
4
// roll number;
5
s1.setSORT_KEY("120");
6
 
          
7
final List<FakeStudent> item1 = EHelper.querySortKeyGreaterThanOrEqualTo(s1,null,true);



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.

Happy Coding!

Java (programming language) Abstraction (computer science) Database

Opinions expressed by DZone contributors are their own.

Related

  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook