0

I am trying to implement a backend DynamoDB for my Spring Boot application. But AWS recently updated their SDKs for DynamoDB. Therefore, almost all of the tutorials available on the internet, such as http://www.baeldung.com/spring-data-dynamodb, aren't directly relevant.

I've read through Amazon's SDK documentation regarding the DynamoDB class. Specifically, the way the object is instantiated and endpoints/regions set have been altered. In the past, constructing and setting endpoints would look like this:

@Bean
public AmazonDynamoDB amazonDynamoDB() {
    AmazonDynamoDB amazonDynamoDB 
      = new AmazonDynamoDBClient(amazonAWSCredentials());

    if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
        amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
    }

    return amazonDynamoDB;
}

@Bean
public AWSCredentials amazonAWSCredentials() {
    return new BasicAWSCredentials(
      amazonAWSAccessKey, amazonAWSSecretKey);
}

However, the setEndpoint() method is now deprecated, and [AWS documentation][1] states that we should construct the DynamoDB object through a builder:

AmazonDynamoDBClient() Deprecated. use AmazonDynamoDBClientBuilder.defaultClient()

This other StackOverflow post recommends using this strategy to instantiate the database connection object:

DynamoDB dynamoDB = new DynamoDB(AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build());

Table table = dynamoDB.getTable("Movies");

But I get an error on IntelliJ that DynamoDB is abstract and cannot be instantiated. But I cannot find any documentation on the proper class to extend.

In other words, I've scoured through tutorials, SO, and the AWS documentation, and haven't found what I believe is the correct way to create my client. Can someone provide an implementation that works? I'm specifically trying to set up a client with a local DynamoDB (endpoint at localhost port 8000).

Community
  • 1
  • 1
Yu Chen
  • 6,540
  • 6
  • 51
  • 86

1 Answers1

4

I think I can take a stab at answering my own question. Using the developer guide here for DynamoDB Mapper you can implement a DynamoDB Mapper object that takes in your client and performs data services for you, like loading, querying, deleting, saving (essentially CRUD?). Here's the documentation I found helpful.

I created my own class called DynamoDBMapperClient with this code:

    private AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
            new EndpointConfiguration(amazonDynamoDBEndpoint, amazonAWSRegion)).build();

private AWSCredentials awsCredentials = new AWSCredentials() {
    @Override
    public String getAWSAccessKeyId() {
        return null;
    }

    @Override
    public String getAWSSecretKey() {
        return null;
    }
};
private DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDB);

public DynamoDBMapper getMapper() {
    return mapper;
}

Basically takes in endpoint and region configurations from a properties file, then instantiates a new mapper that is accessed with a getter.

I know this may not be the complete answer, so I'm leaving this unanswered, but at least it's a start and you guys can tell me what I'm doing wrong!

Yu Chen
  • 6,540
  • 6
  • 51
  • 86