$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-dynamodbProvides Amazon DynamoDB patterns using AWS SDK for Java 2.x. Use when creating, querying, scanning, or performing CRUD operations on DynamoDB tables, working with indexes, batch operations, transactions, or integrating with Spring Boot applications.
| 1 | # AWS SDK for Java 2.x - Amazon DynamoDB |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Provides DynamoDB patterns using AWS SDK for Java 2.x with Enhanced Client for type-safe CRUD, queries, batch operations, transactions, and Spring Boot integration. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - CRUD operations on DynamoDB items |
| 10 | - Querying tables with sort keys or GSI |
| 11 | - Batch operations for multiple items |
| 12 | - Atomic transactions across tables |
| 13 | - Spring Boot integration with DynamoDB |
| 14 | |
| 15 | ## Instructions |
| 16 | |
| 17 | 1. Add AWS SDK DynamoDB dependencies to `pom.xml` |
| 18 | 2. Configure client setup (low-level or Enhanced Client) |
| 19 | 3. Define entity classes with `@DynamoDbBean` annotations |
| 20 | 4. Perform operations using `DynamoDbTable` (CRUD, query, scan, batch, transactions) |
| 21 | 5. Handle partial failures with retry logic and exponential backoff |
| 22 | 6. Use repository pattern for Spring Boot integration |
| 23 | |
| 24 | ## Dependencies |
| 25 | |
| 26 | Add to `pom.xml`: |
| 27 | ```xml |
| 28 | <!-- Low-level DynamoDB client --> |
| 29 | <dependency> |
| 30 | <groupId>software.amazon.awssdk</groupId> |
| 31 | <artifactId>dynamodb</artifactId> |
| 32 | </dependency> |
| 33 | |
| 34 | <!-- Enhanced client (recommended) --> |
| 35 | <dependency> |
| 36 | <groupId>software.amazon.awssdk</groupId> |
| 37 | <artifactId>dynamodb-enhanced</artifactId> |
| 38 | </dependency> |
| 39 | ``` |
| 40 | ## Client Setup |
| 41 | |
| 42 | ### Low-Level Client |
| 43 | ```java |
| 44 | import software.amazon.awssdk.regions.Region; |
| 45 | import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 46 | |
| 47 | DynamoDbClient dynamoDb = DynamoDbClient.builder() |
| 48 | .region(Region.US_EAST_1) |
| 49 | .build(); |
| 50 | ``` |
| 51 | |
| 52 | ### Enhanced Client (Recommended) |
| 53 | ```java |
| 54 | import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; |
| 55 | |
| 56 | DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() |
| 57 | .dynamoDbClient(dynamoDb) |
| 58 | .build(); |
| 59 | ``` |
| 60 | |
| 61 | ## Entity Mapping |
| 62 | |
| 63 | ```java |
| 64 | @DynamoDbBean |
| 65 | public class Customer { |
| 66 | |
| 67 | @DynamoDbPartitionKey |
| 68 | private String customerId; |
| 69 | |
| 70 | @DynamoDbAttribute("customer_name") |
| 71 | private String name; |
| 72 | |
| 73 | private String email; |
| 74 | |
| 75 | @DynamoDbSortKey |
| 76 | private String orderId; |
| 77 | |
| 78 | // Getters and setters |
| 79 | } |
| 80 | ``` |
| 81 | |
| 82 | For complex entity mapping with GSIs and custom converters, see [Entity Mapping Reference](references/entity-mapping.md). |
| 83 | |
| 84 | ## CRUD Operations |
| 85 | |
| 86 | ### Basic Operations |
| 87 | ```java |
| 88 | // Create or update item |
| 89 | DynamoDbTable<Customer> table = enhancedClient.table("Customers", TableSchema.fromBean(Customer.class)); |
| 90 | table.putItem(customer); |
| 91 | |
| 92 | // Get item |
| 93 | Customer result = table.getItem(Key.builder().partitionValue(customerId).build()); |
| 94 | |
| 95 | // Update item |
| 96 | return table.updateItem(customer); |
| 97 | |
| 98 | // Delete item |
| 99 | table.deleteItem(Key.builder().partitionValue(customerId).build()); |
| 100 | ``` |
| 101 | |
| 102 | ### Composite Key Operations |
| 103 | ```java |
| 104 | // Get item with composite key |
| 105 | Order order = table.getItem(Key.builder() |
| 106 | .partitionValue(customerId) |
| 107 | .sortValue(orderId) |
| 108 | .build()); |
| 109 | ``` |
| 110 | |
| 111 | ## Query Operations |
| 112 | |
| 113 | ### Basic Query |
| 114 | ```java |
| 115 | import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional; |
| 116 | |
| 117 | QueryConditional queryConditional = QueryConditional |
| 118 | .keyEqualTo(Key.builder() |
| 119 | .partitionValue(customerId) |
| 120 | .build()); |
| 121 | |
| 122 | List<Order> orders = table.query(queryConditional).items().stream() |
| 123 | .collect(Collectors.toList()); |
| 124 | ``` |
| 125 | |
| 126 | ### Advanced Query with Filters |
| 127 | ```java |
| 128 | import software.amazon.awssdk.enhanced.dynamodb.Expression; |
| 129 | |
| 130 | Expression filter = Expression.builder() |
| 131 | .expression("status = :pending") |
| 132 | .putExpressionValue(":pending", AttributeValue.builder().s("PENDING").build()) |
| 133 | .build(); |
| 134 | |
| 135 | List<Order> pendingOrders = table.query(r -> r |
| 136 | .queryConditional(queryConditional) |
| 137 | .filterExpression(filter)) |
| 138 | .items().stream() |
| 139 | .collect(Collectors.toList()); |
| 140 | ``` |
| 141 | |
| 142 | For detailed query patterns, see [Advanced Operations Reference](references/advanced-operations.md). |
| 143 | |
| 144 | ## Scan Operations |
| 145 | |
| 146 | > **Warning**: Scan reads entire table and consumes read capacity for all items. Prefer Query operations with partition keys or GSIs whenever possible. |
| 147 | |
| 148 | **Validation before scan**: |
| 149 | - Confirm query with partition key is not feasible for your access pattern |
| 150 | - Verify table has sufficient provisioned read capacity or use on-demand mode |
| 151 | - Consider using pagination with `limit()` to control capacity consumption |
| 152 | |
| 153 | ```java |
| 154 | // Scan all items |
| 155 | List<Customer> allCustomers = table.scan().items().stream() |
| 156 | .collect(Collectors.toList()); |
| 157 | |
| 158 | // Scan with filter |
| 159 | Expression filter = Expression.builder() |
| 160 | .expression("points >= :minPoints") |
| 161 | .putExpressionValue(":minPoints", AttributeValue.builder().n("1000").build()) |
| 162 | .build(); |
| 163 | |
| 164 | List<Customer> vipCustomers = table.scan(r -> r.filterExpression(filter)) |
| 165 | .items().stream() |
| 166 | .collect(Collectors.toList()); |
| 167 | ``` |
| 168 | |
| 169 | ## Batch Operations |
| 170 | |
| 171 | ### Batch Get |
| 172 | ```java |
| 173 | import software.amazon.awssdk.enhanced.dynamodb.model.*; |
| 174 | |
| 175 | List<Key> keys = cus |