Use the DynamoDB Enhanced Client API asynchronously
If your application requires non-blocking, asynchronous calls to DynamoDB, you can use the
DynamoDbEnhancedAsyncClient
-
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(); -
Methods that return a single data object return a
CompletableFutureof 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 asynchronousgetItem()method.CompletableFuture<Customer> result = customerDynamoDbTable.getItem(customer); // Perform other work here. return result.join(); // Now block and wait for the result. -
Methods that return paginated lists of results return an
SdkPublisherinstead of an SdkIterablethat the synchronous DynamoDbEnhanceClientreturns 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 asynchronousscan()method of this guide.