Use the DynamoDB Enhanced Client API asynchronously - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use the DynamoDB Enhanced Client API asynchronously

If your application requires non-blocking, asynchronous calls to DynamoDB, you can use the DynamoDbEnhancedAsyncClient. It's similar to the synchronous implementation but with the following key differences:

  1. When you build the DynamoDbEnhancedAsyncClient, you must provide the asynchronous version of the standard client, DynamoDbAsyncClient, as shown in the following snippet.

    DynamoDbEnhancedAsyncClient enhancedClient = DynamoDbEnhancedAsyncClient.builder() .dynamoDbClient(dynamoDbAsyncClient) .build();
  2. Methods that return a single data object return a CompletableFuture of the result instead of only the result. Your application can then do other work without having to block on the result. The following snippet shows the asynchronous getItem() method.

    CompletableFuture<Customer> result = customerDynamoDbTable.getItem(customer); // Perform other work here. return result.join(); // Now block and wait for the result.
  3. Methods that return paginated lists of results return an SdkPublisher instead of an SdkIterable that the synchronous DynamoDbEnhanceClient returns for the same methods. Your application can then subscribe a handler to that publisher to deal with the results asynchronously without having to block.

    PagePublisher<Customer> results = customerDynamoDbTable.query(r -> r.queryConditional(keyEqualTo(k -> k.partitionValue("Smith")))); results.subscribe(myCustomerResultsProcessor); // Perform other work and let the processor handle the results asynchronously.

    For a more complete example of working with the SdkPublisher API, see the example in the section that discusses the asynchronous scan() method of this guide.