BatchGetItem lets you get many items from one or more tables in a single request. It saves time and effort compared to asking for each item one by one.
0
0
BatchGetItem in DynamoDB
Introduction
You want to fetch multiple records by their keys quickly.
You need to get data from different tables at once.
You want to reduce the number of calls to the database to save time.
You have a list of user IDs and want to get their profiles in one go.
You want to load details for many products by their IDs together.
Syntax
DynamoDB
BatchGetItem {
RequestItems: {
TableName1: {
Keys: [ {PrimaryKeyName: {S|N|B: value}}, ... ],
ProjectionExpression: "attribute1, attribute2", # optional
ConsistentRead: true|false # optional
},
TableName2: { ... }
},
ReturnConsumedCapacity: "INDEXES"|"TOTAL"|"NONE" # optional
}Keys is an array of primary key objects to fetch.
ProjectionExpression lets you get only certain attributes to save data transfer.
Examples
Get two users by their UserId from the Users table.
DynamoDB
BatchGetItem {
RequestItems: {
"Users": {
Keys: [ { "UserId": {"S": "123"} }, { "UserId": {"S": "456"} } ]
}
}
}Get product names and prices for two products and one order by their IDs from two tables.
DynamoDB
BatchGetItem {
RequestItems: {
"Products": {
Keys: [ { "ProductId": {"S": "A1"} }, { "ProductId": {"S": "B2"} } ],
ProjectionExpression: "Name, Price"
},
"Orders": {
Keys: [ { "OrderId": {"S": "O100"} } ]
}
}
}Sample Program
This command fetches the UserId and Email attributes for two users with IDs 'user1' and 'user2' from the Users table.
DynamoDB
aws dynamodb batch-get-item --request-items '{
"Users": {
"Keys": [
{"UserId": {"S": "user1"}},
{"UserId": {"S": "user2"}}
],
"ProjectionExpression": "UserId, Email"
}
}'OutputSuccess
Important Notes
BatchGetItem can return fewer items than requested if some keys are not found.
If you request too many items, DynamoDB may return UnprocessedKeys to retry later.
Use ProjectionExpression to reduce data size and improve speed.
Summary
BatchGetItem fetches multiple items from one or more tables in one request.
It uses primary keys to find items quickly.
It helps reduce the number of calls and speeds up data retrieval.