AWS SDK for Java
Developer Guide

Working with Items in DynamoDB

In DynamoDB, an item is a collection of attributes, each of which has a name and a value. An attribute value can be a scalar, set, or document type. For more information, see Naming Rules and Data Types in the Amazon DynamoDB Developer Guide.

Retrieve (Get) an Item from a Table

Call the DynamoDbClient's getItem method and pass it a GetItemRequest object with the table name and primary key value of the item you want. It returns a GetItemResponse object with all of the attributes for that item. You can specify one or more projection expressions in the GetItemRequest to retrieve specific attributes.

You can use the returned GetItemResponse object's item() method to retrieve a Map of key (String) and value (AttributeValue) pairs that are associated with the item.

Imports

import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;

Code

HashMap<String,AttributeValue> key_to_get = new HashMap<String,AttributeValue>(); key_to_get.put("Name", AttributeValue.builder() .s(name).build()); GetItemRequest request = null; if (projection_expression != null) { request = GetItemRequest.builder() .key(key_to_get) .tableName(table_name) .projectionExpression(projection_expression) .build(); } else { request = GetItemRequest.builder() .key(key_to_get) .tableName(table_name) .build(); } DynamoDbClient ddb = DynamoDbClient.create(); try { Map<String,AttributeValue> returned_item = ddb.getItem(request).item(); if (returned_item != null) { Set<String> keys = returned_item.keySet(); for (String key : keys) { System.out.format("%s: %s\n", key, returned_item.get(key).toString()); } } else { System.out.format("No item found with the key %s!\n", name); } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); }

See the complete example on GitHub.

Add a New Item to a Table

Create a Map of key-value pairs that represent the item's attributes. These must include values for the table's primary key fields. If the item identified by the primary key already exists, its fields are updated by the request.

Note

If the named table doesn't exist for your account and region, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;

Code

HashMap<String,AttributeValue> item_values = new HashMap<String,AttributeValue>(); item_values.put("Name", AttributeValue.builder().s(name).build()); for (String[] field : extra_fields) { item_values.put(field[0], AttributeValue.builder().s(field[1]).build()); } DynamoDbClient ddb = DynamoDbClient.create(); PutItemRequest request = PutItemRequest.builder() .tableName(table_name) .item(item_values) .build(); try { ddb.putItem(request); } catch (ResourceNotFoundException e) { System.err.format("Error: The table \"%s\" can't be found.\n", table_name); System.err.println("Be sure that it exists and that you've typed its name correctly!"); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!");

See the complete example on GitHub.

Update an Existing Item in a Table

You can update an attribute for an item that already exists in a table by using the DynamoDbClient's updateItem method, providing a table name, primary key value, and a map of fields to update.

Note

If the named table doesn't exist for your account and region, or if the item identified by the primary key you passed in doesn't exist, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeAction; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

Code

HashMap<String,AttributeValue> item_key = new HashMap<String,AttributeValue>(); item_key.put("Name", AttributeValue.builder().s(name).build()); HashMap<String,AttributeValueUpdate> updated_values = new HashMap<String,AttributeValueUpdate>(); for (String[] field : extra_fields) { updated_values.put(field[0], AttributeValueUpdate.builder() .value(AttributeValue.builder().s(field[1]).build()) .action(AttributeAction.PUT) .build()); } UpdateItemRequest request = UpdateItemRequest.builder() .tableName(table_name) .key(item_key) .attributeUpdates(updated_values) .build(); DynamoDbClient ddb = DynamoDbClient.create(); try { ddb.updateItem(request); } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!");

See the complete example on GitHub.

More Info