DynamoDBMapper is a high-level abstraction provided by the AWS SDK for Java (specifically, the AWS DynamoDB SDK for Java) that simplifies the process of working with Amazon DynamoDB tables in Java applications.

DynamoDBMapper CRUD operations example

It allows you to map your application’s Java objects to items in DynamoDB tables, and it handles much of the underlying DynamoDB interaction for you. This abstraction helps reduce the amount of boilerplate code you need to write when working with DynamoDB, making it more convenient for Java developers.

Here’s a live example of using DynamoDBMapper:

  1. Maven Dependency:
    Make sure you include the DynamoDB SDK for Java dependency in your Maven project:
       <version>2.x.x</version> <!-- Replace with the latest version -->
  1. Java Class:
    Create a simple Java class representing an entity that you want to store in DynamoDB. For example, let’s create a Person class:
   import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
   import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;

   public class Person {
       private String id;
       private String name;

       public String getId() {
           return id;

       public void setId(String id) {
           this.id = id;

       public String getName() {
           return name;

       public void setName(String name) {
           this.name = name;
  1. Usage with DynamoDBMapper:
    Now, you can use DynamoDBMapper to interact with DynamoDB. For example, let’s perform basic CRUD operations:
   import software.amazon.awssdk.enhanced.dynamodb.DynamoDbClient;
   import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
   import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
   import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags;
   import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey;
   import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

   public class DynamoDBMapperExample {

       public static void main(String[] args) {
           // Create a DynamoDB client
           DynamoDbClient dynamoDbClient = DynamoDbClient.create();

           // Create an enhanced DynamoDB client
           DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()

           // Create a DynamoDBMapper
           DynamoDbMapper mapper = DynamoDbMapper.builder()

           // Create a table schema for the Person class
           TableSchema<Person> tableSchema = TableSchema.fromBean(Person.class);

           // Create a Person object
           Person person = new Person();
           person.setName("John Doe");

           // Save the person object to DynamoDB

           // Load the person object from DynamoDB
           Person loadedPerson = mapper.load(Person.class, "1");

           // Update the person's name
           loadedPerson.setName("Updated Name");

           // Query DynamoDB for persons with a specific name
           Person queryPerson = new Person();
           queryPerson.setName("Updated Name");

           DynamoDbQueryExpression<Person> queryExpression = DynamoDbQueryExpression.builder()
                                   .eq(AttributeValue.builder().s("Updated Name").build())

           PaginatedList<Person> queryResults = mapper.query(Person.class, queryExpression);

           for (Person resultPerson : queryResults) {
               System.out.println("Query Result: " + resultPerson);

           // Delete the person from DynamoDB

In this example, we create a Person class, use DynamoDBMapper to save, load, update, query, and delete a Person object in DynamoDB. Note that this is a simplified example, and in a real-world scenario, you might want to handle errors, implement appropriate error handling, and follow best practices for DynamoDB interactions.

Always refer to the official documentation for the latest information.

Categories: DynamoDB


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *