What is the difference between DynamoDBMapper and Table for DynamoDB Tables
G

1

7

In AWS DynamoDB, There are two options available to do the CRUD operations on the Table.

DynamoDBMapper : com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;.

    AmazonDynamoDB dbClient = AmazonDynamoDBAsyncClientBuilder.standard().withCredentials(creds)
            .withRegion("us-east-1").build();
    // creds is AWSCredentialsProvider

    DynamoDBMapper mapper = new DynamoDBMapper(dbClient);
    mapper.save(item);

Table: com.amazonaws.services.dynamodbv2.document.Table;.

static DynamoDB dynamoDB =new DynamoDB(dbClient);
Table table = dynamoDB.getTable("TABLE_NAME");
Item item =new Item().withPrimaryKey("","")
        .withString("":, "");
table.putItem(item);

Both seem to do the same operations.

Is DynamoDBMapper a layer over Table? If so what are the differences in using each of these?

Grajeda answered 21/5, 2021 at 5:41 Comment(0)
W
7

If you want to map Java classes to DynamoDB tables (which is a useful feature), consider moving away from the old V1 API (com.amazonaws.services.dynamodbv2 is V1). V2 packages are software.amazon.awssdk.services.dynamodb.*.

Replace this old API with the DynamoDB V2 Enhanced Client. You can learn about this here:

Map items in DynamoDB tables

You can find code examples for using the Enhanced Client here.

Here is a Java V2 code example that shows you how to use the Enhanced Client to put data into a Customer table. As you see, you can map a Java Class to columns in a DynamoDB table and then create a Customer object when adding data to the table.

package com.example.dynamodb;


import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;


/*
 * Prior to running this code example, create an Amazon DynamoDB table named Customer with a key named id and populate it with data.
 * Also, ensure that you have setup your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class EnhancedPutItem {

    public static void main(String[] args) {

        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        putRecord(enhancedClient) ;
        ddb.close();
    }

    
    // Puts an item into a DynamoDB table
    public static void putRecord(DynamoDbEnhancedClient enhancedClient) {

        try {
            DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));

            // Create an Instant
            LocalDate localDate = LocalDate.parse("2020-04-07");
            LocalDateTime localDateTime = localDate.atStartOfDay();
            Instant instant = localDateTime.toInstant(ZoneOffset.UTC);

            // Populate the Table
            Customer custRecord = new Customer();
            custRecord.setCustName("Susan Blue");
            custRecord.setId("id103");
            custRecord.setEmail("[email protected]");
            custRecord.setRegistrationDate(instant) ;

            // Put the customer data into a DynamoDB table
            custTable.putItem(custRecord);

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        System.out.println("done");
    }


    @DynamoDbBean
    public static class Customer {

        private String id;
        private String name;
        private String email;
        private Instant regDate;

        @DynamoDbPartitionKey
        public String getId() {
            return this.id;
        };

        public void setId(String id) {

            this.id = id;
        }

        @DynamoDbSortKey
        public String getCustName() {
            return this.name;

        }

        public void setCustName(String name) {

            this.name = name;
        }

        public String getEmail() {
            return this.email;
        }

        public void setEmail(String email) {

            this.email = email;
        }

        public Instant getRegistrationDate() {
            return regDate;
        }
        public void setRegistrationDate(Instant registrationDate) {

            this.regDate = registrationDate;
        }
    }
    
}
Wineshop answered 21/5, 2021 at 12:54 Comment(7)
Ok, I understand that i should use the latest version of the package. But the question still remains. DynamoDBMapper or Table and Why?Grajeda
And the new package has DynamoDbTable, not the DynamoDBMapper right?Grajeda
I posted an example - so in the example, you can see V2 Enhanced Client uses a object named DynamoDbTable.Wineshop
That is how i like to help in this DEV community. Get the community aware of the latest AWS SDK for Java V2. Using V2 is best practice. All code in V2 is tested a lot and works. Thanks!Wineshop
But the thing is with the name "dynamodbv2" most people would think that it is Version 2.Grajeda
agreed - its confusing and has been made aware to the SDK team.Wineshop
A tip -- all V2 packages start with "software.amazon.awssdk"Wineshop

© 2022 - 2024 — McMap. All rights reserved.