AWS code-library documentation change
Summary
Added batch operation examples for querying, updating, and deleting multiple items
Security assessment
Demonstrates bulk data operations without any security context. Routine feature 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 dbecbb352..7aed400d3 100644 --- a//code-library/latest/ug/java_2_dynamodb_code_examples.md +++ b//code-library/latest/ug/java_2_dynamodb_code_examples.md @@ -2208,0 +2209,387 @@ For complete source code and instructions on how to set up and run, see the full +The following code example shows how to compare multiple values with a single attribute in DynamoDB. + + * Use the IN operator to compare multiple values with a single attribute. + + * Compare the IN operator with multiple OR conditions. + + * Understand the performance and expression complexity benefits of using IN. + + + + +**SDK for Java 2.x** + + +Compare multiple values with a single attribute in DynamoDB using AWS SDK for Java 2.x. + + + 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 software.amazon.awssdk.services.dynamodb.model.ScanRequest; + import software.amazon.awssdk.services.dynamodb.model.ScanResponse; + + import java.util.ArrayList; + import java.util.HashMap; + import java.util.List; + import java.util.Locale; + import java.util.Map; + + /** + * Queries a table using the IN operator to compare multiple values with a single attribute. + * + * <p>This method demonstrates how to use the IN operator in a filter expression + * to match an attribute against multiple values. + * + * @param dynamoDbClient The DynamoDB client + * @param tableName The name of the DynamoDB table + * @param partitionKeyName The name of the partition key attribute + * @param partitionKeyValue The value of the partition key to query + * @param attributeName The name of the attribute to compare + * @param valuesList List of values to compare against + * @return The query response from DynamoDB + * @throws DynamoDbException if an error occurs during the operation + */ + public static QueryResponse compareMultipleValues( + DynamoDbClient dynamoDbClient, + String tableName, + String partitionKeyName, + AttributeValue partitionKeyValue, + String attributeName, + List<AttributeValue> valuesList) { + + // Create expression attribute names + Map<String, String> expressionAttributeNames = new HashMap<>(); + expressionAttributeNames.put("#pkName", partitionKeyName); + expressionAttributeNames.put("#attrName", attributeName); + + // Create expression attribute values + Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); + expressionAttributeValues.put(":pkValue", partitionKeyValue); + + // Add values for IN operator + for (int i = 0; i < valuesList.size(); i++) { + expressionAttributeValues.put(":val" + i, valuesList.get(i)); + } + + // Build the IN clause + StringBuilder inClause = new StringBuilder(); + for (int i = 0; i < valuesList.size(); i++) { + if (i > 0) { + inClause.append(", "); + } + inClause.append(":val").append(i); + } + + // Define the query parameters + QueryRequest request = QueryRequest.builder() + .tableName(tableName) + .keyConditionExpression("#pkName = :pkValue") + .filterExpression("#attrName IN (" + inClause.toString() + ")") + .expressionAttributeNames(expressionAttributeNames) + .expressionAttributeValues(expressionAttributeValues) + .build(); + + // Perform the query operation + return dynamoDbClient.query(request); + } + + /** + * Queries a table using multiple OR conditions to compare multiple values with a single attribute. + * + * <p>This method demonstrates the alternative approach to using the IN operator, + * by using multiple OR conditions. + * + * @param dynamoDbClient The DynamoDB client + * @param tableName The name of the DynamoDB table + * @param partitionKeyName The name of the partition key attribute + * @param partitionKeyValue The value of the partition key to query + * @param attributeName The name of the attribute to compare + * @param valuesList List of values to compare against + * @return The query response from DynamoDB + * @throws DynamoDbException if an error occurs during the operation + */ + public static QueryResponse compareWithOrConditions( + DynamoDbClient dynamoDbClient, + String tableName, + String partitionKeyName, + AttributeValue partitionKeyValue, + String attributeName, + List<AttributeValue> valuesList) { + + // Create expression attribute names + Map<String, String> expressionAttributeNames = new HashMap<>(); + expressionAttributeNames.put("#pkName", partitionKeyName); + expressionAttributeNames.put("#attrName", attributeName); + + // Create expression attribute values + Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); + expressionAttributeValues.put(":pkValue", partitionKeyValue); + + // Add values for OR conditions + for (int i = 0; i < valuesList.size(); i++) { + expressionAttributeValues.put(":val" + i, valuesList.get(i)); + } + + // Build the OR conditions + StringBuilder orConditions = new StringBuilder(); + for (int i = 0; i < valuesList.size(); i++) { + if (i > 0) { + orConditions.append(" OR "); + } + orConditions.append("#attrName = :val").append(i); + } + + // Define the query parameters + QueryRequest request = QueryRequest.builder() + .tableName(tableName) + .keyConditionExpression("#pkName = :pkValue") + .filterExpression(orConditions.toString()) + .expressionAttributeNames(expressionAttributeNames) + .expressionAttributeValues(expressionAttributeValues) + .build(); + + // Perform the query operation + return dynamoDbClient.query(request); + } + + /** + * Compares the performance of using the IN operator versus multiple OR conditions. + * + * <p>This method demonstrates the performance difference between using the IN operator + * and using multiple OR conditions. + * + * @param dynamoDbClient The DynamoDB client + * @param tableName The name of the DynamoDB table + * @param partitionKeyName The name of the partition key attribute + * @param partitionKeyValue The value of the partition key to query + * @param attributeName The name of the attribute to compare + * @param valuesList List of values to compare against + * @return Map containing the performance comparison results + */ + public static Map<String, Object> comparePerformance( + DynamoDbClient dynamoDbClient, + String tableName, + String partitionKeyName, + AttributeValue partitionKeyValue, + String attributeName, + List<AttributeValue> valuesList) { + + Map<String, Object> results = new HashMap<>(); + + try { + // Measure performance of IN operator + long inStartTime = System.nanoTime(); + QueryResponse inResponse = compareMultipleValues( + dynamoDbClient, tableName, partitionKeyName, partitionKeyValue, attributeName, valuesList); + long inEndTime = System.nanoTime(); + long inDuration = inEndTime - inStartTime; + + // Measure performance of OR conditions + long orStartTime = System.nanoTime(); + QueryResponse orResponse = compareWithOrConditions( + dynamoDbClient, tableName, partitionKeyName, partitionKeyValue, attributeName, valuesList); + long orEndTime = System.nanoTime(); + long orDuration = orEndTime - orStartTime; + + // Record results + results.put("inOperatorDuration", inDuration); + results.put("orConditionsDuration", orDuration); + results.put("inOperatorItems", inResponse.count()); + results.put("orConditionsItems", orResponse.count()); + results.put("inOperatorExpression", "IN operator with " + valuesList.size() + " values"); + results.put("orConditionsExpression", valuesList.size() + " OR conditions");