2

在 AWS DynamoDB 中,有两个选项可用于对表执行 CRUD 操作。

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);

表: 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);

两者似乎都执行相同的操作。

DynamoDBMapperTable之上的一层吗?如果是这样,使用它们有什么区别?

4

1 回答 1

1

如果您想将 Java 类映射到 DynamoDB 表(这是一个有用的功能),请考虑放弃旧的 V1 API(com.amazonaws.services.dynamodbv2是 V1)。V2 软件包是software.amazon.awssdk.services.dynamodb。*。

用 DynamoDB V2 增强客户端替换这个旧 API。你可以在这里了解这一点:

映射 DynamoDB 表中的项目

您可以在此处找到使用增强客户端的代码示例。

这是一个 Java V2 代码示例,它向您展示了如何使用增强型客户端将数据放入客户表中。如您所见,您可以将 Java 类映射到 DynamoDB 表中的列,然后在向表中添加数据时创建Customer对象。

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("sblue@noserver.com");
            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;
        }
    }
    
}
于 2021-05-21T12:54:31.697 回答