import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.stream.Collectors; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest; import software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse; import software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; private List findByIds(List eventIds) { // Prepare the list of key maps for the batch get request. // Assumes the table's partition key is named "eventId". List> keys = new ArrayList<>(); for (String eventId : eventIds) { Map key = new HashMap<>(); key.put("eventId", AttributeValue.builder().s(eventId).build()); keys.add(key); } // Specify the projection expression to retrieve only "attribute1" and "attribute2". // If these attribute names conflict with DynamoDB reserved words, // you may need to use ExpressionAttributeNames. String projectionExpression = "attribute1, attribute2"; // Build the KeysAndAttributes object with keys and the projection. KeysAndAttributes keysAndAttributes = KeysAndAttributes.builder() .keys(keys) .projectionExpression(projectionExpression) .build(); // Create the request items map with the table name as the key. // Assumes cgfpUpstreamEventTable is an instance of DynamoDbTable. Map requestItems = new HashMap<>(); requestItems.put(cgfpUpstreamEventTable.tableName(), keysAndAttributes); // Build and execute the low-level batchGetItem request. BatchGetItemRequest batchGetItemRequest = BatchGetItemRequest.builder() .requestItems(requestItems) .build(); BatchGetItemResponse response = dynamoDbClient.batchGetItem(batchGetItemRequest); // Retrieve the list of returned items (as maps of AttributeValue). List> items = response.responses().get(cgfpUpstreamEventTable.tableName()); // Use the table's TableSchema to convert the raw attribute maps into DAO objects. TableSchema tableSchema = cgfpUpstreamEventTable.tableSchema(); // Map each item to an instance of CgfpUpstreamEventDAO. return items.stream() .map(tableSchema::mapToItem) .collect(Collectors.toList()); }