Get Multiple Items in DynamoDB Using Java (Guide w/ Code Examples)
![Rafal Wilinski](https://pbs.twimg.com/profile_images/1390706809548197895/SVCKpLNE_400x400.png)
Provided by Rafal Wilinski
To retrieve multiple items from a DynamoDB table in a single operation in Java, you can use the batchGetItem
method of the AmazonDynamoDB
client. In this example, YourTableName
is the name of the table which you want to retrieve the items from.
The batchGetItem
method accepts a BatchGetItemRequest
object, which takes a map of the table name to KeysAndAttributes
objects. Each KeysAndAttributes
object represents the keys and attributes of the items that you want to retrieve.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.KeysAndAttributes; import com.amazonaws.services.dynamodbv2.model.batchGetItemRequest; import com.amazonaws.services.dynamodbv2.model.Attributevalue; import com.amazonaws.services.dynamodbv2.model.batchGetItemResult; public class DynamoDBBatchGetExample { public static void main(String[] args) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>(); List<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>(); keys.add(new HashMap<String, AttributeValue>() { { put("id", new AttributeValue().withN("1")); } }); keys.add(new HashMap<String, AttributeValue>() { { put("id", new AttributeValue().withN("2")); } }); requestItems.put("YourTableName", new KeysAndAttributes().withKeys(keys)); BatchGetItemRequest request = new BatchGetItemRequest() .withRequestItems(requestItems); BatchGetItemResult result = client.batchGetItem(request); System.out.println("Items retrieved successfully!"); System.out.println(result.get()); } }
Similar Code Examples
- Update Item in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Query Hash Key Only in DynamoDB Using Java
- Delete Multiple Items in DynamoDB Using Java
- Conditional Update in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Update Multiple Attributes in DynamoDB Using Java
- Batch Put Item in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
- Batch Get in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- Delete by Hashkey Using DynamoDB Mapper
Login to the AWS Console less. Use Dynobase.
Try 7-day free trial. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase