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:
   <dependency>
       <groupId>software.amazon.awssdk</groupId>
       <artifactId>dynamodb</artifactId>
       <version>2.x.x</version> <!-- Replace with the latest version -->
   </dependency>
  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;

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

       @DynamoDbPartitionKey
       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()
                   .dynamoDbClient(dynamoDbClient)
                   .build();

           // Create a DynamoDBMapper
           DynamoDbMapper mapper = DynamoDbMapper.builder()
                   .dynamoDbClient(dynamoDbClient)
                   .build();

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

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

           // Save the person object to DynamoDB
           mapper.save(person);

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

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

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

           DynamoDbQueryExpression<Person> queryExpression = DynamoDbQueryExpression.builder()
                   .queryConditional(
                           DynamoDbQueryConditional
                                   .attribute("name")
                                   .eq(AttributeValue.builder().s("Updated Name").build())
                   )
                   .build();

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

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

           // Delete the person from DynamoDB
           mapper.delete(loadedPerson);
       }
   }

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

0 Comments

Leave a Reply

Avatar placeholder

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