dynobase-icon
Dynobase

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

Rafal Wilinski

Provided by Rafal Wilinski

To insert multiple items into a DynamoDB table in a single batch operation in Java, you can use the batchWriteItem method of the AmazonDynamoDB client. Here YourTableName is the table name where you want to insert the items.

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.writeitemrequest; import com.amazonaws.services.dynamodbv2.model.batchwriteitemrequest; import com.amazonaws.services.dynamodbv2.model.batchwriteitemresult; public class DynamoDBBatchInsertExample { 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.

Try 7-day free trial. No credit card needed.

Product Features

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