Receive messages/events from SQS and map them to Dynamodb

134
January 26, 2022, at 04:20 AM

I am trying to figure out a way where I can read my SQS events and map them to DynamoDB using Java. I have a SQS in AWS which will get some messages having some information e.g. {"id": "xxx", "state": "xxx"}. These events I want to map them in my DynamoDB table. The DynamoDB contains id as a partition key. According to my reasearch this is doable by reading messages from sqs and use putItem from AWS DynamoDB in JAVA. Is there any simple way to that using JAVA. Or any example would be helpful here.

Thank you !

Answer 1

For AWS, there is a JAVA SDK 2.0 specifically design for this:

Generic Example of Receiving Messages from an SQS Queue:

    ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
        .queueUrl(queueUrl)
        .maxNumberOfMessages(5)
        .build();
    List<Message> messages = sqsClient.receiveMessage(receiveMessageRequest).messages();

https://github.com/awsdocs/aws-doc-sdk-examples/blob/fb99a9b5453c61c13a032b9c225986c865ba4705/javav2/example_code/sqs/src/main/java/com/example/sqs/SQSExample.java#L152

Generic Example of Putting an Item into DynamoDB:

 HashMap<String,AttributeValue> itemValues = new HashMap<String,AttributeValue>();
// Add all content to the table
itemValues.put(key, AttributeValue.builder().s(keyVal).build());
itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
itemValues.put(albumTitle, AttributeValue.builder().s(albumTitleValue).build());
itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
PutItemRequest request = PutItemRequest.builder()
        .tableName(tableName)
        .item(itemValues)
        .build();
ddb.putItem(request);
System.out.println(tableName +" was successfully updated");

https://github.com/awsdocs/aws-doc-sdk-examples/blob/fb99a9b5453c61c13a032b9c225986c865ba4705/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/PutItem.java#L84

AWS documentation is excellent and so are its examples:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/fb99a9b5453c61c13a032b9c225986c865ba4705/javav2

Official Documentation: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/prog-services.html

Rent Charter Buses Company
READ ALSO
Why does jQuery or a DOM method such as getElementById not find the element?

Why does jQuery or a DOM method such as getElementById not find the element?

What are the possible reasons for documentgetElementById, $("#id") or any other DOM method / jQuery selector not finding the elements?

46
How to remove multiple children in one call - Realtime Firebase

How to remove multiple children in one call - Realtime Firebase

I wan to delete multiple children with known keyI searched all the related posts but it doesn't seem to work

122
Image data not saving to MongoDB

Image data not saving to MongoDB

Preface: using Nodejs w/Express to build a blog website and using multer to upload an image to mongoDB

123
Is it possible to make MYSQL table to be read only(cannot modify the details)

Is it possible to make MYSQL table to be read only(cannot modify the details)

Hello may I ask if it is possible to make a MYSQL table to read only? In which I had a table for student records which is to be updated whenever a user/student registers which saves details from the MYSQL database which is non-modifiable due to being read onlyIf...

128