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. GetItem will not accept a partial key.
  • projectionExpression — optional; limits which attributes come back (saves bandwidth, not read cost). Every projected attribute is aliased (#proj0#proj3) via expressionAttributeNames, which keeps reserved words like Year safe.
  • item() — when no item matches, the response carries an empty map (never null); check isEmpty() 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.

References

Last verified 2026-07-13 against the official AWS documentation linked above.

Lavora con DynamoDB senza la Console

DynoTable è un client desktop veloce per DynamoDB — sfoglia le tabelle, esegui query in stile SQL e modifica gli Item localmente.