2

I want to write items to DynamoDB using AWS Lambda written in Java. I use the following code to write to DynamoDB one record at a time:

public void writeToDynamoDB() {
   AmazonDynamoDB client = AmazonDyanmoDBClientBuilder.defaultClient();
   Map<String, AttributeValue> itemValues = new HashMap<>();
   itemValues.put("partitionKey", new AttributeValue("123"));
   client.putItem("DynamoDBTable", itemValues);    
}

But it turns out that I need to write the records in batches. How can I do it. Is there any modification I can make to this code?

2

1 Answer 1

1

When you want to perform AWS tasks with Java, check the Java Developer V2 Dev Guide located here.

Developer guide - AWS SDK for Java 2.x

The Amazon Java SDK team recommends moving to V2.

Amazon recommends using AWS SDK for Java 2.x, which is a major rewrite of the 1.11.x code base built on top of Java 8+. Java SDK 2.x has improved consistency, ease of use, and strongly enforced immutability. It also has support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.

Now to perform the task that you want, use the DynamoDB API V2 and the Enhanced Client. You can find information here.

See this example that demonstrates how to insert many items into an Amazon DynamoDB table by using the enhanced client.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/enhanced/EnhancedBatchWriteItems.java

You can use the Java Lambda runtime API and the DynamoDB API to create a Lambda function to meet your requirements.

Sign up to request clarification or add additional context in comments.

2 Comments

github link is broken.
Fixed it - the Enhanced Client examples where moved to a sub package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.