Search code examples
javaamazon-web-servicesamazon-dynamodbamazon-sqs

Receive messages/events from SQS and map them to Dynamodb


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 !


Solution

  • 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