Search code examples
javaamazon-web-servicesaws-lambdaamazon-dynamodbamazon-dynamodb-streams

DynamoDB: How to read actual field value from AttributeValue?


I am using AWS Lambda written in java to process DynamoDB Streams. Here is the code:

public class DDBEventProcessor implements
        RequestHandler<DynamodbEvent, String> {

    public String handleRequest(DynamodbEvent ddbEvent, Context context) {
        for (DynamodbStreamRecord record : ddbEvent.getRecords()){
            Map<String, AttributeValue> tmp = record.getDynamodb.getNewImage();   
            
            //convert tmp to Map<String, Object> here
        }
    }
}

I need to process the record further.

For that it needs to be converted to a Map<String, Object>.
The data received will be of the format: { "id":{"s":"777"}}.
But I want to convert it into {"id":"777"}, so that I am able to parse the map object easily. But there are nested arrays and maps as well. Also the record is very complicated consisting of nested arrays and maps. How to achieve this?


Solution

  • This can be achieved easily with an inbuilt class ItemUtils. Check the implementation of toSimpleMapValue. The implementation is recursive in nature and it traverses over the schema of Map<String, AttributeValue> and converts it into a simple map object. I simply copied the required methods essential for the transformation.