dynobase-icon
Dynobase

Batch Put Item in DynamoDB Using Java (Guide w/ Code Examples)

Rafal Wilinski

Provided by Rafal Wilinski

You can use the batchWriteItem method of the AmazonDynamoDB client to put multiple items into a DynamoDB table in a single batch operation in Java.

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.Attribute.Attributevalue; import com.amazonaws.services.dynamodbv2.model.putrequest; import com.amazonaws.services.dynamodbv2.model.batchwriteitemrequest; import com.amazonaws.services.dynamodbv2.model.batchwriteitemresult; public class DynamoDBBatchPutExample { public static void main(String[] args) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>(); List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(); writeRequests.add(new WriteRequest(new PutRequest( new Item().withPrimaryKey("id", 1).withString("name", "John") .withNumber("age", 30)))); writeRequests.add(new WriteRequest(new PutRequest( new Item().withPrimaryKey("id", 2).withString("name", "Jane") .withNumber("age", 25)))); requestItems.put("YourTableName", writeRequests); BatchWriteItemRequest request = new BatchWriteItemRequest() .withRequestItems(requestItems); BatchWriteItemResult result = client.batchWriteItem(request); System.out.println("Items inserted successfully!"); } }

Tired of AWS Console? Try Dynobase.

First 7 days are on us. No strings attached.

Product Features

Download
/
Changelog
/
Pricing
/
Member Portal
/
Privacy
/
EULA
/
Twitter
/
Affiliates & Influencers
© 2024 Dynobase
+
Still using AWS DynamoDB Console?
Try Dynobase to accelerate your DynamoDB workflow. Start your 7-day free trial today.