AWS code-library documentation change
Summary
Added nested attribute query example with path parsing logic
Security assessment
Functionality expansion without security implications
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 b4e58f0da..dbecbb352 100644 --- a//code-library/latest/ug/java_2_dynamodb_code_examples.md +++ b//code-library/latest/ug/java_2_dynamodb_code_examples.md @@ -1390,3 +1390,2 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - final DescribeTimeToLiveRequest request = DescribeTimeToLiveRequest.builder() - .tableName(tableName) - .build(); + final DescribeTimeToLiveRequest request = + DescribeTimeToLiveRequest.builder().tableName(tableName).build(); @@ -1394,2 +1393,3 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for Java - try (DynamoDbClient ddb = dynamoDbClient != null ? dynamoDbClient : - DynamoDbClient.builder().region(region).build()) { + try (DynamoDbClient ddb = dynamoDbClient != null + ? dynamoDbClient + : DynamoDbClient.builder().region(region).build()) { @@ -1724,5 +1724 @@ The following code example shows how to use `Query`. - * Use the begins_with function in a key condition expression. - - * Filter items based on a prefix pattern in the sort key. - - +**SDK for Java 2.x** @@ -1731 +1727 @@ The following code example shows how to use `Query`. -**SDK for Java 2.x** +###### Note @@ -1732,0 +1729 @@ 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). @@ -1734 +1731 @@ The following code example shows how to use `Query`. -Query a DynamoDB table using a begins_with condition on the sort key with AWS SDK for Java 2.x. +Queries a table by using [DynamoDbClient](http://docs.aws.amazon.com/sdk-for-java/latest/reference/software/amazon/awssdk/services/dynamodb/DynamoDbClient.html). @@ -1736,0 +1734 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD + import software.amazon.awssdk.regions.Region; @@ -1742,2 +1739,0 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; - @@ -1745,3 +1740,0 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - import java.util.Map; - import java.util.logging.Level; - import java.util.logging.Logger; @@ -1749,6 +1742,15 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - public QueryResponse queryWithBeginsWithCondition( - final String tableName, - final String partitionKeyName, - final String partitionKeyValue, - final String sortKeyName, - final String sortKeyPrefix) { + /** + * 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 = """ @@ -1756,3 +1758,2 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - CodeSampleUtils.validateTableParameters(tableName, partitionKeyName, partitionKeyValue); - CodeSampleUtils.validateStringParameter("Sort key name", sortKeyName); - CodeSampleUtils.validateStringParameter("Sort key prefix", sortKeyPrefix); + Usage: + <tableName> <partitionKeyName> <partitionKeyVal> @@ -1760,4 +1761,5 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - // 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); + 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). + """; @@ -1765,4 +1767,4 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - // 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()); + if (args.length != 3) { + System.out.println(usage); + System.exit(1); + } @@ -1770,2 +1772,33 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - // Create the query request - final QueryRequest queryRequest = QueryRequest.builder() + 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()); + + QueryRequest queryReq = QueryRequest.builder() @@ -1773,3 +1806,3 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - .keyConditionExpression(KEY_CONDITION_EXPRESSION) - .expressionAttributeNames(expressionAttributeNames) - .expressionAttributeValues(expressionAttributeValues) + .keyConditionExpression(partitionAlias + " = :" + partitionKeyName) + .expressionAttributeNames(attrNameAlias) + .expressionAttributeValues(attrValues) @@ -1779,6 +1812,3 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - 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; + QueryResponse response = ddb.query(queryReq); + return response.count(); + @@ -1786,2 +1816,4 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD - LOGGER.log(Level.SEVERE, "Error querying with begins_with condition", e); - throw e; + System.err.println(e.getMessage()); + System.exit(1); + } + return -1; @@ -1793 +1825 @@ Query a DynamoDB table using a begins_with condition on the sort key with AWS SD -Demonstrate using begins_with with different prefix lengths with AWS SDK for Java 2.x. +Queries a table by using `DynamoDbClient` and a secondary index. @@ -1795,0 +1828,25 @@ Demonstrate using begins_with with different prefix lengths with AWS SDK for Jav + 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 { @@ -1797,5 +1854,5 @@ Demonstrate using begins_with with different prefix lengths with AWS SDK for Jav - try { - CodeSampleUtils.BeginsWithQueryConfig config = CodeSampleUtils.BeginsWithQueryConfig.fromArgs(args); - LOGGER.log(Level.INFO, "Querying items where {0} = {1} and {2} begins with ''{3}''", - new Object[]{config.getPartitionKeyName(), config.getPartitionKeyValue(), - config.getSortKeyName(), config.getSortKeyPrefix()}); + String tableName = "Movies"; + Region region = Region.US_EAST_1; + DynamoDbClient ddb = DynamoDbClient.builder() + .region(region) + .build(); @@ -1803,9 +1860,3 @@ Demonstrate using begins_with with different prefix lengths with AWS SDK for Jav - // Using the builder pattern to create and execute the query - final QueryResponse response = new BeginsWithQueryBuilder() - .withTableName(config.getTableName()) - .withPartitionKeyName(config.getPartitionKeyName()) - .withPartitionKeyValue(config.getPartitionKeyValue()) - .withSortKeyName(config.getSortKeyName()) - .withSortKeyPrefix(config.getSortKeyPrefix()) - .withRegion(config.getRegion()) - .execute(); + queryIndex(ddb, tableName); + ddb.close(); + }