AWS Security ChangesHomeSearch

AWS amazondynamodb documentation change

Service: amazondynamodb · 2026-01-19 · Documentation low

File: amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.md

Summary

Updated Java code example to migrate from AWS SDK for Java v1 to v2, including package changes, modernized API calls, and syntax improvements.

Security assessment

The changes are purely technical updates to use the newer SDK version without introducing any security-specific features or addressing vulnerabilities. No security-related context is present in the diff.

Diff

diff --git a/amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.md b/amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.md
index 05b1f2a30..9c70cd281 100644
--- a//amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.md
+++ b//amazondynamodb/latest/developerguide/LSIJavaDocumentAPI.Example.md
@@ -31,26 +31 @@ For step-by-step instructions for testing the following sample, see [Java code e
-    package com.amazonaws.codesamples.document;
-    
-    import java.util.ArrayList;
-    import java.util.Iterator;
-    
-    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
-    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
-    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
-    import com.amazonaws.services.dynamodbv2.document.Index;
-    import com.amazonaws.services.dynamodbv2.document.Item;
-    import com.amazonaws.services.dynamodbv2.document.ItemCollection;
-    import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
-    import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
-    import com.amazonaws.services.dynamodbv2.document.Table;
-    import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
-    import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
-    import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
-    import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
-    import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
-    import com.amazonaws.services.dynamodbv2.model.KeyType;
-    import com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex;
-    import com.amazonaws.services.dynamodbv2.model.Projection;
-    import com.amazonaws.services.dynamodbv2.model.ProjectionType;
-    import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
-    import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
-    import com.amazonaws.services.dynamodbv2.model.Select;
+    package com.example.dynamodb;
@@ -58 +33,4 @@ For step-by-step instructions for testing the following sample, see [Java code e
-    public class DocumentAPILocalSecondaryIndexExample {
+    import software.amazon.awssdk.core.waiters.WaiterResponse;
+    import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
+    import software.amazon.awssdk.services.dynamodb.model.*;
+    import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
@@ -60,2 +38,2 @@ For step-by-step instructions for testing the following sample, see [Java code e
-            static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
-            static DynamoDB dynamoDB = new DynamoDB(client);
+    import java.util.HashMap;
+    import java.util.Map;
@@ -63 +41 @@ For step-by-step instructions for testing the following sample, see [Java code e
-            public static String tableName = "CustomerOrders";
+    public class DocumentAPILocalSecondaryIndexExample {
@@ -65 +43,2 @@ For step-by-step instructions for testing the following sample, see [Java code e
-            public static void main(String[] args) throws Exception {
+        static DynamoDbClient client = DynamoDbClient.create();
+        public static String tableName = "CustomerOrders";
@@ -66,0 +46 @@ For step-by-step instructions for testing the following sample, see [Java code e
+        public static void main(String[] args) {
@@ -79,77 +56,35 @@ For step-by-step instructions for testing the following sample, see [Java code e
-    
-                    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
-                                    .withProvisionedThroughput(
-                                                    new ProvisionedThroughput().withReadCapacityUnits((long) 1)
-                                                                    .withWriteCapacityUnits((long) 1));
-    
-                    // Attribute definitions for table partition and sort keys
-                    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
-                    attributeDefinitions
-                                    .add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S"));
-                    attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N"));
-    
-                    // Attribute definition for index primary key attributes
-                    attributeDefinitions
-                                    .add(new AttributeDefinition().withAttributeName("OrderCreationDate")
-                                                    .withAttributeType("N"));
-                    attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N"));
-    
-                    createTableRequest.setAttributeDefinitions(attributeDefinitions);
-    
-                    // Key schema for table
-                    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
-                    tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
-                                                                                                                          // key
-                    tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE)); // Sort
-                                                                                                                        // key
-    
-                    createTableRequest.setKeySchema(tableKeySchema);
-    
-                    ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
-    
-                    // OrderCreationDateIndex
-                    LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex()
-                                    .withIndexName("OrderCreationDateIndex");
-    
-                    // Key schema for OrderCreationDateIndex
-                    ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
-                    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
-                                                                                                                          // key
-                    indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate")
-                                    .withKeyType(KeyType.RANGE)); // Sort
-                                                                  // key
-    
-                    orderCreationDateIndex.setKeySchema(indexKeySchema);
-    
-                    // Projection (with list of projected attributes) for
-                    // OrderCreationDateIndex
-                    Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
-                    ArrayList<String> nonKeyAttributes = new ArrayList<String>();
-                    nonKeyAttributes.add("ProductCategory");
-                    nonKeyAttributes.add("ProductName");
-                    projection.setNonKeyAttributes(nonKeyAttributes);
-    
-                    orderCreationDateIndex.setProjection(projection);
-    
-                    localSecondaryIndexes.add(orderCreationDateIndex);
-    
-                    // IsOpenIndex
-                    LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex");
-    
-                    // Key schema for IsOpenIndex
-                    indexKeySchema = new ArrayList<KeySchemaElement>();
-                    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
-                                                                                                                          // key
-                    indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE)); // Sort
-                                                                                                                       // key
-    
-                    // Projection (all attributes) for IsOpenIndex
-                    projection = new Projection().withProjectionType(ProjectionType.ALL);
-    
-                    isOpenIndex.setKeySchema(indexKeySchema);
-                    isOpenIndex.setProjection(projection);
-    
-                    localSecondaryIndexes.add(isOpenIndex);
-    
-                    // Add index definitions to CreateTable request
-                    createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);
+            CreateTableRequest request = CreateTableRequest.builder()
+                .tableName(tableName)
+                .provisionedThroughput(ProvisionedThroughput.builder()
+                    .readCapacityUnits(1L)
+                    .writeCapacityUnits(1L)
+                    .build())
+                .attributeDefinitions(
+                    AttributeDefinition.builder().attributeName("CustomerId").attributeType(ScalarAttributeType.S).build(),
+                    AttributeDefinition.builder().attributeName("OrderId").attributeType(ScalarAttributeType.N).build(),
+                    AttributeDefinition.builder().attributeName("OrderCreationDate").attributeType(ScalarAttributeType.N).build(),
+                    AttributeDefinition.builder().attributeName("IsOpen").attributeType(ScalarAttributeType.N).build())
+                .keySchema(
+                    KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(),
+                    KeySchemaElement.builder().attributeName("OrderId").keyType(KeyType.RANGE).build())
+                .localSecondaryIndexes(
+                    LocalSecondaryIndex.builder()
+                        .indexName("OrderCreationDateIndex")
+                        .keySchema(
+                            KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(),
+                            KeySchemaElement.builder().attributeName("OrderCreationDate").keyType(KeyType.RANGE).build())
+                        .projection(Projection.builder()
+                            .projectionType(ProjectionType.INCLUDE)
+                            .nonKeyAttributes("ProductCategory", "ProductName")
+                            .build())
+                        .build(),
+                    LocalSecondaryIndex.builder()
+                        .indexName("IsOpenIndex")
+                        .keySchema(
+                            KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(),
+                            KeySchemaElement.builder().attributeName("IsOpen").keyType(KeyType.RANGE).build())
+                        .projection(Projection.builder()
+                            .projectionType(ProjectionType.ALL)
+                            .build())
+                        .build())
+                .build();
@@ -158,9 +93,5 @@ For step-by-step instructions for testing the following sample, see [Java code e
-                    System.out.println(dynamoDB.createTable(createTableRequest));
-    
-                    // Wait for table to become active
-                    System.out.println("Waiting for " + tableName + " to become ACTIVE...");
-                    try {
-                            Table table = dynamoDB.getTable(tableName);
-                            table.waitForActive();
-                    } catch (InterruptedException e) {
-                            e.printStackTrace();
+            client.createTable(request);
+    
+            try (DynamoDbWaiter waiter = client.waiter()) {
+                WaiterResponse<DescribeTableResponse> response = waiter.waitUntilTableExists(r -> r.tableName(tableName));
+                response.matched().response().ifPresent(System.out::println);
@@ -171,3 +101,0 @@ For step-by-step instructions for testing the following sample, see [Java code e
-    
-                    Table table = dynamoDB.getTable(tableName);
-    
@@ -177,5 +105 @@ For step-by-step instructions for testing the following sample, see [Java code e
-                    QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true)
-                                    .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
-    
-                    if (indexName == "IsOpenIndex") {
-    
+            if ("IsOpenIndex".equals(indexName)) {
@@ -184,5 +107,0 @@ For step-by-step instructions for testing the following sample, see [Java code e
-                            Index index = table.getIndex(indexName);
-    
-                            querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen")
-                                            .withValueMap(new ValueMap().withString(":v_custid", "[email protected]")
-                                                            .withNumber(":v_isopen", 1));
@@ -190,2 +109,3 @@ For step-by-step instructions for testing the following sample, see [Java code e
-                            querySpec.withProjectionExpression(
-                                            "OrderCreationDate, ProductCategory, ProductName, OrderStatus");