DynamoDB GetItem in Java (AWS SDK v2)
GetItem reads a single item by its full primary key. In AWS SDK for Java 2.x you build a GetItemRequest and call getItem on a DynamoDbClient; every key value is an AttributeValue built with the typed builder (.s(...) for strings, .n(...) for numbers).
Code
import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
public class GetItemExample {
public static void main(String[] args) {
try (DynamoDbClient ddb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build()) {
Map<String, AttributeValue> key = new HashMap<>();
key.put("Artist", AttributeValue.builder().s("Arturo Sandoval").build());
key.put("SongTitle", AttributeValue.builder().s("Cubano Chant").build());
Map<String, String> names = new HashMap<>();
names.put("#proj0", "Artist");
names.put("#proj1", "SongTitle");
names.put("#proj2", "AlbumTitle");
names.put("#proj3", "Year");
GetItemRequest request = GetItemRequest.builder()
.tableName("Music")
.key(key)
.projectionExpression("#proj0, #proj1, #proj2, #proj3")
.expressionAttributeNames(names)
.build();
Map<String, AttributeValue> item = ddb.getItem(request).item();
if (item.isEmpty()) {
System.out.println("Item not found");
} else {
System.out.println(item);
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
}
}
}Prefer plain Java objects over AttributeValue maps? The DynamoDB Enhanced Client (software.amazon.awssdk.enhanced.dynamodb) maps items to annotated bean classes on top of the same low-level calls.
Explanation
tableName— the table to read from.key— must include every primary-key attribute: the partition key alone for a simple key, or partition and sort key for a composite key.GetItemwill not accept a partial key.projectionExpression— optional; limits which attributes come back (saves bandwidth, not read cost). Every projected attribute is aliased (#proj0…#proj3) viaexpressionAttributeNames, which keeps reserved words likeYearsafe.item()— when no item matches, the response carries an empty map (nevernull); checkisEmpty()before using it.- By default reads are eventually consistent. Add
.consistentRead(true)for a strongly-consistent read (costs 2× the RCU, base table only). - The client is
AutoCloseable— the try-with-resources block releases its HTTP connections.
Do it visually
Building the projection or a conditional read by hand? Use the free DynamoDB Expression Builder to assemble the expression and copy the exact ProjectionExpression / ExpressionAttributeNames values.
To browse tables and run GetItem in a GUI — key form, results grid, one-click copy of the generated code — download DynoTable.
Related examples
- DynamoDB GetItem in Go — the same read with AWS SDK for Go v2.
- DynamoDB Query in Java — read a whole partition instead of one item.
- How DynamoDB partition keys work — why
GetItemneeds the full key. - DynamoDB ResourceNotFoundException — the usual first error here: wrong table name or region.
- "The provided key element does not match the schema" — the key you pass doesn't match the table's key schema.
References
- GetItem — Amazon DynamoDB API Reference
- Use GetItem with an AWS SDK or CLI — Amazon DynamoDB Developer Guide
- DynamoDbClient — AWS SDK for Java 2.x API Reference
- GetItemRequest — AWS SDK for Java 2.x API Reference
- Read consistency — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.