Batch Read in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
BatchGetItem reads several known full primary keys, including keys from different partitions. Handle UnprocessedKeys separately from returned items and do not assume response order matches request order.
This example uses Java 17+ and the software.amazon.awssdk:dynamodb SDK 2.x dependency. Configure an AWS Region and credentials through your usual AWS profile or runtime role. The Orders table has String keys pk and sk; adapt the table and keys to your schema.
import java.util.*; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; public class Example { static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } static Map<String, AttributeValue> key(String order) { return Map.of("pk", s("CUSTOMER#123"), "sk", s(order)); } public static void main(String[] args) throws InterruptedException { try (DynamoDbClient ddb = DynamoDbClient.create()) { Map<String, KeysAndAttributes> pending = Map.of("Orders", KeysAndAttributes.builder().keys(key("ORDER#001"), key("ORDER#002")).build()); for (int attempt = 0; !pending.isEmpty(); attempt++) { if (attempt == 8) throw new IllegalStateException("Unprocessed keys: " + pending); if (attempt > 0) Thread.sleep((long) (Math.random() * Math.min(5000L, 100L << attempt))); BatchGetItemResponse response = ddb.batchGetItem(BatchGetItemRequest.builder() .requestItems(pending).build()); response.responses().getOrDefault("Orders", List.of()).forEach(System.out::println); pending = response.unprocessedKeys(); } } } }
Use at most 100 distinct keys per request. Size limits or throttling can produce a partial response even below that count. A nonexistent item is omitted, not returned as an error.
Reference: AWS BatchGetItem API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- Delete in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Get All Items from DynamoDB Using Java
- Update Multiple Attributes in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
- Query Date Range in DynamoDB Using Java
- Batch Query in DynamoDB Using Java
- Query Hash Key Only in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Remove Attribute in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today