AWS amazondynamodb documentation change
Summary
Refactored Java code example for creating DynamoDB tables with GSIs: removed main method and data loading/querying logic, converted string literals to constants, improved code structure and formatting
Security assessment
Changes focus on code quality improvements and example simplification. No security controls, vulnerabilities, or security features are added/modified. Changes involve using constants instead of literals and restructuring code flow without security implications.
Diff
diff --git a/amazondynamodb/latest/developerguide/example_dynamodb_CreateTableWithGlobalSecondaryIndex_section.md b/amazondynamodb/latest/developerguide/example_dynamodb_CreateTableWithGlobalSecondaryIndex_section.md index 5f42fef23..899c5031d 100644 --- a//amazondynamodb/latest/developerguide/example_dynamodb_CreateTableWithGlobalSecondaryIndex_section.md +++ b//amazondynamodb/latest/developerguide/example_dynamodb_CreateTableWithGlobalSecondaryIndex_section.md @@ -18,5 +17,0 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - import java.util.ArrayList; - import java.util.HashMap; - import java.util.List; - import java.util.Map; - @@ -35 +29,0 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - import software.amazon.awssdk.services.dynamodb.model.Projection; @@ -37,0 +32 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. + import software.amazon.awssdk.services.dynamodb.model.Projection; @@ -46,18 +41,4 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - public class DynamoDbGlobalSecondaryIndexExample { - - private static final String tableName = "Issues"; - private static final DynamoDbClient dynamoDbClient = DynamoDbClient.builder() - .region(Region.US_EAST_1) - .credentialsProvider(DefaultCredentialsProvider.create()) - .build(); - - public static void main(String[] args) throws Exception { - createTable(); - loadData(); - - queryIndex("CreateDateIndex"); - queryIndex("TitleIndex"); - queryIndex("DueDateIndex"); - - deleteTable(tableName); - } + import java.util.ArrayList; + import java.util.HashMap; + import java.util.List; + import java.util.Map; @@ -65 +46 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - public static void createTable() { + public void createTable() { @@ -68,5 +49,17 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); - attributeDefinitions.add(AttributeDefinition.builder().attributeName("IssueId").attributeType(ScalarAttributeType.S).build()); - attributeDefinitions.add(AttributeDefinition.builder().attributeName("Title").attributeType(ScalarAttributeType.S).build()); - attributeDefinitions.add(AttributeDefinition.builder().attributeName("CreateDate").attributeType(ScalarAttributeType.S).build()); - attributeDefinitions.add(AttributeDefinition.builder().attributeName("DueDate").attributeType(ScalarAttributeType.S).build()); + final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName(ISSUE_ID_ATTR) + .attributeType(ScalarAttributeType.S) + .build()); + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName(TITLE_ATTR) + .attributeType(ScalarAttributeType.S) + .build()); + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName(CREATE_DATE_ATTR) + .attributeType(ScalarAttributeType.S) + .build()); + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName(DUE_DATE_ATTR) + .attributeType(ScalarAttributeType.S) + .build()); @@ -75,3 +68,9 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - List<KeySchemaElement> tableKeySchema = new ArrayList<>(); - tableKeySchema.add(KeySchemaElement.builder().attributeName("IssueId").keyType(KeyType.HASH).build()); // Partition key - tableKeySchema.add(KeySchemaElement.builder().attributeName("Title").keyType(KeyType.RANGE).build()); // Sort key + final List<KeySchemaElement> tableKeySchema = new ArrayList<>(); + tableKeySchema.add(KeySchemaElement.builder() + .attributeName(ISSUE_ID_ATTR) + .keyType(KeyType.HASH) + .build()); // Partition key + tableKeySchema.add(KeySchemaElement.builder() + .attributeName(TITLE_ATTR) + .keyType(KeyType.RANGE) + .build()); // Sort key @@ -80 +79 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - ProvisionedThroughput ptIndex = ProvisionedThroughput.builder() + final ProvisionedThroughput ptIndex = ProvisionedThroughput.builder() @@ -86,5 +85,11 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - List<KeySchemaElement> createDateKeySchema = new ArrayList<>(); - createDateKeySchema.add(KeySchemaElement.builder().attributeName("CreateDate").keyType(KeyType.HASH).build()); - createDateKeySchema.add(KeySchemaElement.builder().attributeName("IssueId").keyType(KeyType.RANGE).build()); - - Projection createDateProjection = Projection.builder() + final List<KeySchemaElement> createDateKeySchema = new ArrayList<>(); + createDateKeySchema.add(KeySchemaElement.builder() + .attributeName(CREATE_DATE_ATTR) + .keyType(KeyType.HASH) + .build()); + createDateKeySchema.add(KeySchemaElement.builder() + .attributeName(ISSUE_ID_ATTR) + .keyType(KeyType.RANGE) + .build()); + + final Projection createDateProjection = Projection.builder() @@ -92 +97 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - .nonKeyAttributes("Description", "Status") + .nonKeyAttributes(DESCRIPTION_ATTR, STATUS_ATTR) @@ -95,2 +100,2 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - GlobalSecondaryIndex createDateIndex = GlobalSecondaryIndex.builder() - .indexName("CreateDateIndex") + final GlobalSecondaryIndex createDateIndex = GlobalSecondaryIndex.builder() + .indexName(CREATE_DATE_INDEX) @@ -103,5 +108,11 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - List<KeySchemaElement> titleKeySchema = new ArrayList<>(); - titleKeySchema.add(KeySchemaElement.builder().attributeName("Title").keyType(KeyType.HASH).build()); - titleKeySchema.add(KeySchemaElement.builder().attributeName("IssueId").keyType(KeyType.RANGE).build()); - - Projection titleProjection = Projection.builder() + final List<KeySchemaElement> titleKeySchema = new ArrayList<>(); + titleKeySchema.add(KeySchemaElement.builder() + .attributeName(TITLE_ATTR) + .keyType(KeyType.HASH) + .build()); + titleKeySchema.add(KeySchemaElement.builder() + .attributeName(ISSUE_ID_ATTR) + .keyType(KeyType.RANGE) + .build()); + + final Projection titleProjection = Projection.builder() @@ -111,2 +122,2 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - GlobalSecondaryIndex titleIndex = GlobalSecondaryIndex.builder() - .indexName("TitleIndex") + final GlobalSecondaryIndex titleIndex = GlobalSecondaryIndex.builder() + .indexName(TITLE_INDEX) @@ -119,2 +130,5 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - List<KeySchemaElement> dueDateKeySchema = new ArrayList<>(); - dueDateKeySchema.add(KeySchemaElement.builder().attributeName("DueDate").keyType(KeyType.HASH).build()); + final List<KeySchemaElement> dueDateKeySchema = new ArrayList<>(); + dueDateKeySchema.add(KeySchemaElement.builder() + .attributeName(DUE_DATE_ATTR) + .keyType(KeyType.HASH) + .build()); @@ -122 +136 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - Projection dueDateProjection = Projection.builder() + final Projection dueDateProjection = Projection.builder() @@ -126,2 +140,2 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - GlobalSecondaryIndex dueDateIndex = GlobalSecondaryIndex.builder() - .indexName("DueDateIndex") + final GlobalSecondaryIndex dueDateIndex = GlobalSecondaryIndex.builder() + .indexName(DUE_DATE_INDEX) @@ -133,2 +147,2 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - CreateTableRequest createTableRequest = CreateTableRequest.builder() - .tableName(tableName) + final CreateTableRequest createTableRequest = CreateTableRequest.builder() + .tableName(TABLE_NAME) @@ -144 +158 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - System.out.println("Creating table " + tableName + "..."); + System.out.println("Creating table " + TABLE_NAME + "..."); @@ -148,4 +162,4 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - System.out.println("Waiting for " + tableName + " to become ACTIVE..."); - DynamoDbWaiter waiter = dynamoDbClient.waiter(); - DescribeTableRequest describeTableRequest = DescribeTableRequest.builder() - .tableName(tableName) + System.out.println("Waiting for " + TABLE_NAME + " to become ACTIVE..."); + final DynamoDbWaiter waiter = dynamoDbClient.waiter(); + final DescribeTableRequest describeTableRequest = DescribeTableRequest.builder() + .tableName(TABLE_NAME) @@ -154 +168 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - WaiterResponse<DescribeTableResponse> waiterResponse = waiter.waitUntilTableExists(describeTableRequest); + final WaiterResponse<DescribeTableResponse> waiterResponse = waiter.waitUntilTableExists(describeTableRequest); @@ -164,149 +177,0 @@ Create DynamoDB table with Global Secondary Index using AWS SDK for Java 2.x. - public static void queryIndex(String indexName) { - try { - System.out.println("\n***********************************************************\n"); - System.out.print("Querying index " + indexName + "..."); - - Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); - String keyConditionExpression; - - if (indexName.equals("CreateDateIndex")) { - System.out.println("Issues filed on 2013-11-01"); - keyConditionExpression = "CreateDate = :v_date and begins_with(IssueId, :v_issue)"; - expressionAttributeValues.put(":v_date", AttributeValue.builder().s("2013-11-01").build()); - expressionAttributeValues.put(":v_issue", AttributeValue.builder().s("A-").build()); - } else if (indexName.equals("TitleIndex")) { - System.out.println("Compilation errors"); - keyConditionExpression = "Title = :v_title and begins_with(IssueId, :v_issue)"; - expressionAttributeValues.put(":v_title", AttributeValue.builder().s("Compilation error").build()); - expressionAttributeValues.put(":v_issue", AttributeValue.builder().s("A-").build()); - } else if (indexName.equals("DueDateIndex")) { - System.out.println("Items that are due on 2013-11-30"); - keyConditionExpression = "DueDate = :v_date"; - expressionAttributeValues.put(":v_date", AttributeValue.builder().s("2013-11-30").build()); - } else { - System.out.println("\nNo valid index name provided"); - return; - } - - QueryRequest queryRequest = QueryRequest.builder() - .tableName(tableName) - .indexName(indexName) - .keyConditionExpression(keyConditionExpression) - .expressionAttributeValues(expressionAttributeValues) - .build(); - - QueryResponse response = dynamoDbClient.query(queryRequest); - System.out.println("Query: printing results..."); -