My current project consists of building an Angular frontend to capture a POST request API that both sends out data (such as a list of "image_id", "modules", etc) from my Amazon DynamoDB table or validates the incoming input (also a list of image_id, etc) from Angular, depending on the payload (the backend AWS lambda function is in Java, and my specific task is to build the retrieval of the data). I'm at the stage of the project where I need to connect my lambda function to DynamoDB and acquire specific data (my first data I want to acquire is the "image_id", contained in the "container_master" table). There's one "image_id" key in each item of the table, and I want to be able to gather all of the id's for now. However, I've hit a roadblock. The problem is that the array of "image_id"'s is empty when it's shown on the website's console. I figured that the problem was due to the lambda function's handling of the scanned items in the database, because any other array that is hardcoded and sent to my website is rendering nicely.
HomeScreen_Entity.java:
@DynamoDBTable(tableName = "container_master")public class HomeScreen_Entity { private String image_id; AmazonDynamoDB dynamoDBClient; private Regions REGION = Regions.US_WEST_2; DynamoDBMapper mapper; public HomeScreen_Entity() { dynamoDBClient = AmazonDynamoDBClientBuilder.standard().withRegion(REGION).build(); mapper = new DynamoDBMapper(dynamoDBClient); } @DynamoDBHashKey(attributeName = "image_id") public String getImage_id() { return image_id; } public void setImage_id(String image_id) { this.image_id = image_id; } public List<String> getAllImageIds() { //HomeScreen_Entity.java ScanRequest scanRequest = new ScanRequest() .withTableName("container_master") .withAttributesToGet("image_id"); ScanResult result = dynamoDBClient.scan(scanRequest); List<String> imageIds = new ArrayList<>(); for (Map<String, com.amazonaws.services.dynamodbv2.model.AttributeValue> item : result.getItems()) { if (item.containsKey("image_id")) { imageIds.add(item.get("image_id").getS()); } } }}
The code above handles the scanning of the DynamoDB table.ECS_DAO.java:
public List<HomeScreen_Entity> fetchData(Payload_Entity pe) { //ECS_DAO.java List<HomeScreen_Entity> entities = new ArrayList<>(); try { HomeScreen_Entity homeScreenEntity = new HomeScreen_Entity(); List<String> imageIds = homeScreenEntity.getAllImageIds(); for (String id : imageIds) { HomeScreen_Entity entity = new HomeScreen_Entity(); entity.setImage_id(id); entities.add(entity); } } catch (Exception ex) { ex.printStackTrace(); System.out.println("No records found"); } return entities; }
The code above will handle all general data (for now, I'm focusing on Image ID's.ECS_BO.java:
public List<String> retrieveDataFromDynamoDB(Payload_Entity pe) { //ECS_BO.java ECS_DAO db = new ECS_DAO(); List<HomeScreen_Entity> data = db.fetchData(pe); List<String> imageIds = data.stream() .map(HomeScreen_Entity::getImage_id) .collect(Collectors.toList()); return imageIds; }
The code above converts the HomeScreen_Entity objects into List<String>'s so it can be converted into JSON later and rendered.MainController.java (just for good measure):
case RETRIEVE_DATA: //MainController.java. payload change to "retrieve" instead of "execute"/"running" ECS_BO ecsbo = new ECS_BO(); body = ecsbo.retrieveDataFromDynamoDB(pe); return buildBodyApiResponse(context, 200, body);...
I'm having a tough time finding out why "body" returns an empty array. My guess is that there's an issue with the handling of the scanning of the data. Just as a disclaimer: there are supposed to be a few items in the DynamoDB table. Another disclaimer: image_id is a partition key.