Create an enhanced
client and DynamoDbTable
Create an enhanced client
The DynamoDbEnhancedClient
The enhanced client requires a standard DynamoDbClient
to perform work. The API offers two ways to create a
DynamoDbEnhancedClient
instance. The first option, shown in the following snippet, creates a
standard DynamoDbClient
with default settings picked up from configuration
settings.
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.create();
If you want to configure the underlying standard client, you can supply it to the enhanced client's builder method as shown in the following snippet.
// Configure an instance of the standard DynamoDbClient. DynamoDbClient standardClient = DynamoDbClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); // Use the configured standard client with the enhanced client. DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(standardClient) .build();
Create a
DynamoDbTable
instance
Think of a DynamoDbTable
TableSchema
. The
DynamoDbTable
class provides methods for CRUD operations that let you
interact with a single DynamoDB table.
DynamoDbTable<T>
is a generic class that takes a single type argument,
whether it is a custom class or an EnhancedDocument
when working with
document-type items. This argument type establishes the relationship between the class that
you use and the single DynamoDB table.
Use the table()
factory method of the DynamoDbEnhancedClient
to create a DynamoDbTable
instance as shown in the following snippet.
static final DynamoDbTable<Customer> customerTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
DynamoDbTable
instances are candidates for singletons because they are
immutable and can be used throughout your application.
Your code now has an in-memory representation of a DynamoDB table that can work with
Customer
instances. The actual DynamoDB table might or might not exist. If the
table named Customer
already exists, you can begin performing CRUD operations
against it. If it doesn't exist, use the DynamoDbTable
instance to create the
table as discussed in the next section.