Fast and flexible NoSQL database service with seamless scaling, consistent performance, and built-in security features.
Main interface for Amazon DynamoDB operations including item management, querying, and table operations.
/**
* Amazon DynamoDB client interface
*/
public interface AmazonDynamoDB {
String ENDPOINT_PREFIX = "dynamodb";
// Item operations
PutItemResult putItem(PutItemRequest putItemRequest);
PutItemResult putItem(String tableName, Map<String, AttributeValue> item);
GetItemResult getItem(GetItemRequest getItemRequest);
GetItemResult getItem(String tableName, Map<String, AttributeValue> key);
DeleteItemResult deleteItem(DeleteItemRequest deleteItemRequest);
DeleteItemResult deleteItem(String tableName, Map<String, AttributeValue> key);
UpdateItemResult updateItem(UpdateItemRequest updateItemRequest);
// Query and scan operations
QueryResult query(QueryRequest queryRequest);
ScanResult scan(ScanRequest scanRequest);
ScanResult scan(String tableName, List<String> attributesToGet);
// Batch operations
BatchGetItemResult batchGetItem(BatchGetItemRequest batchGetItemRequest);
BatchWriteItemResult batchWriteItem(BatchWriteItemRequest batchWriteItemRequest);
// Table management
CreateTableResult createTable(CreateTableRequest createTableRequest);
DeleteTableResult deleteTable(DeleteTableRequest deleteTableRequest);
DescribeTableResult describeTable(DescribeTableRequest describeTableRequest);
DescribeTableResult describeTable(String tableName);
UpdateTableResult updateTable(UpdateTableRequest updateTableRequest);
ListTablesResult listTables(ListTablesRequest listTablesRequest);
ListTablesResult listTables();
// Lifecycle management
void shutdown();
}Builder for creating Amazon DynamoDB clients.
/**
* Builder for creating Amazon DynamoDB clients
*/
public final class AmazonDynamoDBClientBuilder extends AwsClientBuilder<AmazonDynamoDBClientBuilder, AmazonDynamoDB> {
/**
* Creates a builder with default configuration
* @return New DynamoDB client builder
*/
public static AmazonDynamoDBClientBuilder standard();
/**
* Creates a default DynamoDB client with standard configuration
* @return Configured DynamoDB client using default credential chain and region
*/
public static AmazonDynamoDB defaultClient();
}Usage Examples:
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.regions.Regions;
// Default DynamoDB client
AmazonDynamoDB dynamoClient = AmazonDynamoDBClientBuilder.defaultClient();
// Custom DynamoDB client
AmazonDynamoDB customDynamoClient = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.build();Core operations for managing items in DynamoDB tables.
/**
* Request for putting an item into a table
*/
public class PutItemRequest extends AmazonWebServiceRequest {
/**
* Creates a request to put an item
* @param tableName Name of the table
* @param item Item attributes to store
*/
public PutItemRequest(String tableName, Map<String, AttributeValue> item);
public String getTableName();
public Map<String, AttributeValue> getItem();
public String getConditionExpression();
public Map<String, String> getExpressionAttributeNames();
public Map<String, AttributeValue> getExpressionAttributeValues();
// Conditional operations
public PutItemRequest withConditionExpression(String conditionExpression);
public PutItemRequest withExpressionAttributeNames(Map<String, String> expressionAttributeNames);
public PutItemRequest withExpressionAttributeValues(Map<String, AttributeValue> expressionAttributeValues);
// Return values
public PutItemRequest withReturnValues(ReturnValue returnValues);
}
/**
* Request for getting an item from a table
*/
public class GetItemRequest extends AmazonWebServiceRequest {
/**
* Creates a request to get an item
* @param tableName Name of the table
* @param key Primary key of the item
*/
public GetItemRequest(String tableName, Map<String, AttributeValue> key);
public String getTableName();
public Map<String, AttributeValue> getKey();
public List<String> getAttributesToGet();
public String getProjectionExpression();
public Boolean getConsistentRead();
public GetItemRequest withAttributesToGet(String... attributesToGet);
public GetItemRequest withProjectionExpression(String projectionExpression);
public GetItemRequest withConsistentRead(Boolean consistentRead);
}
/**
* Attribute value for DynamoDB operations
*/
public class AttributeValue {
/**
* Creates an attribute value with string data
* @param s String value
*/
public AttributeValue(String s);
/**
* Creates an attribute value with number data
* @param n Number value as string
*/
public AttributeValue withN(String n);
/**
* Creates an attribute value with boolean data
* @param bool Boolean value
*/
public AttributeValue withBOOL(Boolean bool);
// Data type accessors
public String getS(); // String
public String getN(); // Number
public Boolean getBOOL(); // Boolean
public List<String> getSS(); // String Set
public List<String> getNS(); // Number Set
public List<AttributeValue> getL(); // List
public Map<String, AttributeValue> getM(); // Map
}Usage Examples:
import com.amazonaws.services.dynamodbv2.model.*;
import java.util.*;
// Put item
Map<String, AttributeValue> item = new HashMap<>();
item.put("userId", new AttributeValue("user123"));
item.put("name", new AttributeValue("John Doe"));
item.put("age", new AttributeValue().withN("30"));
item.put("active", new AttributeValue().withBOOL(true));
PutItemResult putResult = dynamoClient.putItem("Users", item);
// Get item
Map<String, AttributeValue> key = new HashMap<>();
key.put("userId", new AttributeValue("user123"));
GetItemResult getResult = dynamoClient.getItem("Users", key);
Map<String, AttributeValue> retrievedItem = getResult.getItem();
if (retrievedItem != null) {
String name = retrievedItem.get("name").getS();
int age = Integer.parseInt(retrievedItem.get("age").getN());
boolean active = retrievedItem.get("active").getBOOL();
System.out.println("User: " + name + ", Age: " + age + ", Active: " + active);
}
// Conditional put (only if item doesn't exist)
PutItemRequest conditionalPut = new PutItemRequest("Users", item)
.withConditionExpression("attribute_not_exists(userId)");
try {
dynamoClient.putItem(conditionalPut);
System.out.println("Item created successfully");
} catch (ConditionalCheckFailedException e) {
System.out.println("Item already exists");
}Operations for querying items using primary keys and secondary indexes.
/**
* Request for querying items in a table
*/
public class QueryRequest extends AmazonWebServiceRequest {
/**
* Creates a request to query a table
* @param tableName Name of the table to query
*/
public QueryRequest(String tableName);
public String getTableName();
public String getIndexName();
public String getKeyConditionExpression();
public String getFilterExpression();
public String getProjectionExpression();
public Map<String, String> getExpressionAttributeNames();
public Map<String, AttributeValue> getExpressionAttributeValues();
public Integer getLimit();
public Boolean getScanIndexForward();
public Map<String, AttributeValue> getExclusiveStartKey();
// Configuration methods
public QueryRequest withIndexName(String indexName);
public QueryRequest withKeyConditionExpression(String keyConditionExpression);
public QueryRequest withFilterExpression(String filterExpression);
public QueryRequest withProjectionExpression(String projectionExpression);
public QueryRequest withExpressionAttributeNames(Map<String, String> expressionAttributeNames);
public QueryRequest withExpressionAttributeValues(Map<String, AttributeValue> expressionAttributeValues);
public QueryRequest withLimit(Integer limit);
public QueryRequest withScanIndexForward(Boolean scanIndexForward);
public QueryRequest withConsistentRead(Boolean consistentRead);
}
/**
* Result of a query operation
*/
public class QueryResult {
/**
* Gets the items returned by the query
* @return List of items matching the query
*/
public List<Map<String, AttributeValue>> getItems();
/**
* Gets the number of items returned
* @return Count of items
*/
public Integer getCount();
/**
* Gets the number of items evaluated during the query
* @return Scanned count
*/
public Integer getScannedCount();
/**
* Gets the last evaluated key for pagination
* @return Last evaluated key or null if no more items
*/
public Map<String, AttributeValue> getLastEvaluatedKey();
/**
* Gets consumed capacity information
* @return Consumed capacity details
*/
public ConsumedCapacity getConsumedCapacity();
}Usage Examples:
import com.amazonaws.services.dynamodbv2.model.*;
import java.util.*;
// Query items by partition key
Map<String, String> expressionAttributeNames = new HashMap<>();
expressionAttributeNames.put("#pk", "userId");
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":userId", new AttributeValue("user123"));
QueryRequest queryRequest = new QueryRequest("UserPosts")
.withKeyConditionExpression("#pk = :userId")
.withExpressionAttributeNames(expressionAttributeNames)
.withExpressionAttributeValues(expressionAttributeValues)
.withScanIndexForward(false) // Descending order
.withLimit(10); // Limit results
QueryResult queryResult = dynamoClient.query(queryRequest);
System.out.println("Found " + queryResult.getCount() + " posts");
for (Map<String, AttributeValue> item : queryResult.getItems()) {
String postId = item.get("postId").getS();
String title = item.get("title").getS();
String timestamp = item.get("timestamp").getS();
System.out.println("Post: " + postId + " - " + title + " (" + timestamp + ")");
}
// Query with filter expression
QueryRequest filteredQuery = new QueryRequest("UserPosts")
.withKeyConditionExpression("#pk = :userId")
.withFilterExpression("#status = :status")
.withExpressionAttributeNames(Map.of("#pk", "userId", "#status", "status"))
.withExpressionAttributeValues(Map.of(
":userId", new AttributeValue("user123"),
":status", new AttributeValue("published")
));
QueryResult filteredResult = dynamoClient.query(filteredQuery);Operations for scanning entire tables or indexes.
/**
* Request for scanning a table
*/
public class ScanRequest extends AmazonWebServiceRequest {
/**
* Creates a request to scan a table
* @param tableName Name of the table to scan
*/
public ScanRequest(String tableName);
public String getTableName();
public String getIndexName();
public String getFilterExpression();
public String getProjectionExpression();
public Map<String, String> getExpressionAttributeNames();
public Map<String, AttributeValue> getExpressionAttributeValues();
public Integer getLimit();
public Integer getSegment();
public Integer getTotalSegments();
public Map<String, AttributeValue> getExclusiveStartKey();
// Configuration methods
public ScanRequest withIndexName(String indexName);
public ScanRequest withFilterExpression(String filterExpression);
public ScanRequest withProjectionExpression(String projectionExpression);
public ScanRequest withLimit(Integer limit);
public ScanRequest withSegment(Integer segment);
public ScanRequest withTotalSegments(Integer totalSegments);
}
/**
* Result of a scan operation
*/
public class ScanResult {
/**
* Gets the items returned by the scan
* @return List of items matching the scan
*/
public List<Map<String, AttributeValue>> getItems();
/**
* Gets the number of items returned
* @return Count of items
*/
public Integer getCount();
/**
* Gets the number of items evaluated during the scan
* @return Scanned count
*/
public Integer getScannedCount();
/**
* Gets the last evaluated key for pagination
* @return Last evaluated key or null if no more items
*/
public Map<String, AttributeValue> getLastEvaluatedKey();
}Usage Examples:
import com.amazonaws.services.dynamodbv2.model.*;
// Scan table with filter
ScanRequest scanRequest = new ScanRequest("Users")
.withFilterExpression("#age > :minAge AND #active = :active")
.withExpressionAttributeNames(Map.of("#age", "age", "#active", "active"))
.withExpressionAttributeValues(Map.of(
":minAge", new AttributeValue().withN("25"),
":active", new AttributeValue().withBOOL(true)
))
.withProjectionExpression("userId, #name, #age")
.withLimit(50);
ScanResult scanResult = dynamoClient.scan(scanRequest);
System.out.println("Active users over 25: " + scanResult.getCount());
for (Map<String, AttributeValue> item : scanResult.getItems()) {
String userId = item.get("userId").getS();
String name = item.get("name").getS();
int age = Integer.parseInt(item.get("age").getN());
System.out.println("User: " + userId + " - " + name + " (age " + age + ")");
}
// Parallel scan for large tables
int totalSegments = 4;
for (int segment = 0; segment < totalSegments; segment++) {
ScanRequest parallelScan = new ScanRequest("LargeTable")
.withSegment(segment)
.withTotalSegments(totalSegments);
// Process each segment in parallel (use threads for true parallelism)
ScanResult segmentResult = dynamoClient.scan(parallelScan);
// Process segmentResult...
}Operations for creating, managing, and monitoring DynamoDB tables.
/**
* Request for creating a table
*/
public class CreateTableRequest extends AmazonWebServiceRequest {
/**
* Creates a request to create a table
* @param tableName Name of the table
* @param keySchema Primary key schema
*/
public CreateTableRequest(String tableName, List<KeySchemaElement> keySchema);
public String getTableName();
public List<KeySchemaElement> getKeySchema();
public List<AttributeDefinition> getAttributeDefinitions();
public ProvisionedThroughput getProvisionedThroughput();
public List<GlobalSecondaryIndex> getGlobalSecondaryIndexes();
public List<LocalSecondaryIndex> getLocalSecondaryIndexes();
public CreateTableRequest withAttributeDefinitions(AttributeDefinition... attributeDefinitions);
public CreateTableRequest withProvisionedThroughput(ProvisionedThroughput provisionedThroughput);
public CreateTableRequest withGlobalSecondaryIndexes(GlobalSecondaryIndex... globalSecondaryIndexes);
}
/**
* Primary key schema element
*/
public class KeySchemaElement {
/**
* Creates a key schema element
* @param attributeName Name of the attribute
* @param keyType Type of key (HASH or RANGE)
*/
public KeySchemaElement(String attributeName, KeyType keyType);
public String getAttributeName();
public KeyType getKeyType(); // HASH (partition key) or RANGE (sort key)
}
/**
* Attribute definition for table schema
*/
public class AttributeDefinition {
/**
* Creates an attribute definition
* @param attributeName Name of the attribute
* @param attributeType Type of the attribute
*/
public AttributeDefinition(String attributeName, ScalarAttributeType attributeType);
public String getAttributeName();
public ScalarAttributeType getAttributeType(); // S (String), N (Number), B (Binary)
}
/**
* Provisioned throughput settings
*/
public class ProvisionedThroughput {
/**
* Creates provisioned throughput settings
* @param readCapacityUnits Read capacity units per second
* @param writeCapacityUnits Write capacity units per second
*/
public ProvisionedThroughput(Long readCapacityUnits, Long writeCapacityUnits);
public Long getReadCapacityUnits();
public Long getWriteCapacityUnits();
}Usage Examples:
import com.amazonaws.services.dynamodbv2.model.*;
import java.util.*;
// Create table with hash and range key
List<AttributeDefinition> attributeDefinitions = Arrays.asList(
new AttributeDefinition("userId", ScalarAttributeType.S),
new AttributeDefinition("postId", ScalarAttributeType.S),
new AttributeDefinition("timestamp", ScalarAttributeType.N)
);
List<KeySchemaElement> keySchema = Arrays.asList(
new KeySchemaElement("userId", KeyType.HASH), // Partition key
new KeySchemaElement("postId", KeyType.RANGE) // Sort key
);
ProvisionedThroughput throughput = new ProvisionedThroughput(5L, 5L);
CreateTableRequest createRequest = new CreateTableRequest("UserPosts", keySchema)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(throughput);
CreateTableResult createResult = dynamoClient.createTable(createRequest);
TableDescription tableDesc = createResult.getTableDescription();
System.out.println("Created table: " + tableDesc.getTableName() +
", Status: " + tableDesc.getTableStatus());
// Wait for table to become active
String tableName = "UserPosts";
String tableStatus;
do {
Thread.sleep(1000); // Wait 1 second
DescribeTableResult describeResult = dynamoClient.describeTable(tableName);
tableStatus = describeResult.getTable().getTableStatus();
System.out.println("Table status: " + tableStatus);
} while (!tableStatus.equals("ACTIVE"));
// List all tables
ListTablesResult listResult = dynamoClient.listTables();
System.out.println("Tables: " + listResult.getTableNames());/**
* Return value options for DynamoDB operations
*/
public enum ReturnValue {
NONE, // Don't return any values
ALL_OLD, // Return all old attribute values
UPDATED_OLD, // Return only updated old attribute values
ALL_NEW, // Return all new attribute values
UPDATED_NEW // Return only updated new attribute values
}
/**
* Key types for primary key schema
*/
public enum KeyType {
HASH, // Partition key
RANGE // Sort key
}
/**
* Scalar attribute types
*/
public enum ScalarAttributeType {
S, // String
N, // Number
B // Binary
}
/**
* Table description information
*/
public class TableDescription {
public String getTableName();
public String getTableStatus();
public List<KeySchemaElement> getKeySchema();
public List<AttributeDefinition> getAttributeDefinitions();
public ProvisionedThroughputDescription getProvisionedThroughput();
public Date getCreationDateTime();
public Long getTableSizeBytes();
public Long getItemCount();
}
/**
* Consumed capacity information
*/
public class ConsumedCapacity {
public String getTableName();
public Double getCapacityUnits();
public Double getReadCapacityUnits();
public Double getWriteCapacityUnits();
}
/**
* Exception for conditional check failures
*/
public class ConditionalCheckFailedException extends AmazonDynamoDBException {
public ConditionalCheckFailedException(String message);
}