1
votes

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?

1
You can see the example in aws documents. docs.aws.amazon.com/amazondynamodb/latest/developerguide/… - nirvana124
Refer to the AWS Java V2 Dev Guide as i specified below for the latest code examples. - smac2020

1 Answers

1
votes

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/master/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/EnhancedBatchWriteItems.java

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