AWS code-library documentation change
Summary
Modified throughput configuration examples with documentation comments
Security assessment
Added Javadoc-style comments for configuration methods but no security documentation
Diff
diff --git a/code-library/latest/ug/java_2_dynamodb_code_examples.md b/code-library/latest/ug/java_2_dynamodb_code_examples.md index a743a6682..b4e58f0da 100644 --- a//code-library/latest/ug/java_2_dynamodb_code_examples.md +++ b//code-library/latest/ug/java_2_dynamodb_code_examples.md @@ -1386 +1386,2 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - import java.util.Optional; + import java.util.logging.Level; + import java.util.logging.Logger; @@ -1387,0 +1389 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java + public DescribeTimeToLiveResponse describeTTL(final String tableName, final Region region) { @@ -1391,6 +1393,4 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - try (DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build()) { - final DescribeTimeToLiveResponse response = ddb.describeTimeToLive(request); - System.out.println(tableName + " description of time to live is " - + response.toString()); + + try (DynamoDbClient ddb = dynamoDbClient != null ? dynamoDbClient : + DynamoDbClient.builder().region(region).build()) { + return ddb.describeTimeToLive(request); @@ -1398,2 +1398,2 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName); - System.exit(1); + System.err.format(TABLE_NOT_FOUND_ERROR, tableName); + throw e; @@ -1402 +1402,2 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - System.exit(1); + throw e; + } @@ -1404 +1404,0 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - System.exit(0); @@ -1724 +1724 @@ The following code example shows how to use `Query`. -**SDK for Java 2.x** + * Use the begins_with function in a key condition expression. @@ -1725,0 +1726 @@ The following code example shows how to use `Query`. + * Filter items based on a prefix pattern in the sort key. @@ -1727 +1727,0 @@ The following code example shows how to use `Query`. -###### Note @@ -1729 +1728,0 @@ The following code example shows how to use `Query`. -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/dynamodb#code-examples). @@ -1731 +1730,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru -Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-java/latest/reference/software/amazon/awssdk/services/dynamodb/DynamoDbClient.html). + +**SDK for Java 2.x** + + +Query a DynamoDB table using a begins_with condition on the sort key with AWS SDK for Java 2.x. @@ -1734 +1736,0 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - import software.amazon.awssdk.regions.Region; @@ -1740,63 +1742 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - import java.util.HashMap; - - /** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * - * To query items from an Amazon DynamoDB table using the AWS SDK for Java V2, - * its better practice to use the - * Enhanced Client. See the EnhancedQueryRecords example. - */ - public class Query { - public static void main(String[] args) { - final String usage = """ - - Usage: - <tableName> <partitionKeyName> <partitionKeyVal> - - Where: - tableName - The Amazon DynamoDB table to put the item in (for example, Music3). - partitionKeyName - The partition key name of the Amazon DynamoDB table (for example, Artist). - partitionKeyVal - The value of the partition key that should match (for example, Famous Band). - """; - - if (args.length != 3) { - System.out.println(usage); - System.exit(1); - } - - String tableName = args[0]; - String partitionKeyName = args[1]; - String partitionKeyVal = args[2]; - - // For more information about an alias, see: - // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html - String partitionAlias = "#a"; - - System.out.format("Querying %s", tableName); - System.out.println(""); - Region region = Region.US_EAST_1; - DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build(); - - int count = queryTable(ddb, tableName, partitionKeyName, partitionKeyVal, partitionAlias); - System.out.println("There were " + count + " record(s) returned"); - ddb.close(); - } - - public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, - String partitionAlias) { - // Set up an alias for the partition key name in case it's a reserved word. - HashMap<String, String> attrNameAlias = new HashMap<String, String>(); - attrNameAlias.put(partitionAlias, partitionKeyName); - - // Set up mapping of the partition name with the value. - HashMap<String, AttributeValue> attrValues = new HashMap<>(); - attrValues.put(":" + partitionKeyName, AttributeValue.builder() - .s(partitionKeyVal) - .build()); + import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; @@ -1804 +1744,28 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - QueryRequest queryReq = QueryRequest.builder() + import java.util.HashMap; + import java.util.Map; + import java.util.logging.Level; + import java.util.logging.Logger; + + public QueryResponse queryWithBeginsWithCondition( + final String tableName, + final String partitionKeyName, + final String partitionKeyValue, + final String sortKeyName, + final String sortKeyPrefix) { + + CodeSampleUtils.validateTableParameters(tableName, partitionKeyName, partitionKeyValue); + CodeSampleUtils.validateStringParameter("Sort key name", sortKeyName); + CodeSampleUtils.validateStringParameter("Sort key prefix", sortKeyPrefix); + + // Create expression attribute names for the column names + final Map<String, String> expressionAttributeNames = new HashMap<>(); + expressionAttributeNames.put(EXPRESSION_ATTRIBUTE_NAME_PK, partitionKeyName); + expressionAttributeNames.put(EXPRESSION_ATTRIBUTE_NAME_SK, sortKeyName); + + // Create expression attribute values for the column values + final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); + expressionAttributeValues.put(EXPRESSION_ATTRIBUTE_VALUE_PK, AttributeValue.builder().s(partitionKeyValue).build()); + expressionAttributeValues.put(EXPRESSION_ATTRIBUTE_VALUE_SK_PREFIX, AttributeValue.builder().s(sortKeyPrefix).build()); + + // Create the query request + final QueryRequest queryRequest = QueryRequest.builder() @@ -1806,3 +1773,3 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - .keyConditionExpression(partitionAlias + " = :" + partitionKeyName) - .expressionAttributeNames(attrNameAlias) - .expressionAttributeValues(attrValues) + .keyConditionExpression(KEY_CONDITION_EXPRESSION) + .expressionAttributeNames(expressionAttributeNames) + .expressionAttributeValues(expressionAttributeValues) @@ -1812,3 +1779,6 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - QueryResponse response = ddb.query(queryReq); - return response.count(); - + final QueryResponse response = dynamoDbClient.query(queryRequest); + LOGGER.log(Level.INFO, "Query with begins_with condition successful. Found {0} items", response.count()); + return response; + } catch (ResourceNotFoundException e) { + LOGGER.log(Level.SEVERE, "Table not found: {0}", tableName); + throw e; @@ -1816,4 +1786,2 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav - System.err.println(e.getMessage()); - System.exit(1); - } - return -1; + LOGGER.log(Level.SEVERE, "Error querying with begins_with condition", e); + throw e; @@ -1825 +1793 @@ Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-jav -Queries a table by using `DynamoDbClient` and a secondary index. +Demonstrate using begins_with with different prefix lengths with AWS SDK for Java 2.x. @@ -1828,25 +1795,0 @@ Queries a table by using `DynamoDbClient` and a secondary index. - import software.amazon.awssdk.regions.Region; - import software.amazon.awssdk.services.dynamodb.DynamoDbClient; - import software.amazon.awssdk.services.dynamodb.model.AttributeValue; - import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; - import software.amazon.awssdk.services.dynamodb.model.QueryRequest; - import software.amazon.awssdk.services.dynamodb.model.QueryResponse; - import java.util.HashMap; - import java.util.Map; - - /** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * - * Create the Movies table by running the Scenario example and loading the Movie - * data from the JSON file. Next create a secondary - * index for the Movies table that uses only the year column. Name the index - * **year-index**. For more information, see: - * - * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html - */ - public class QueryItemsUsingIndex { @@ -1854,11 +1796,0 @@ Queries a table by using `DynamoDbClient` and a secondary index. - String tableName = "Movies";