Batch Put Item in DynamoDB Using Java (Guide w/ Code Examples)
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!"); } }
Similar Code Examples
- Query Global Secondary Index in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Get in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Conditional Update in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Query Hash Key Only in DynamoDB Using Java
- Delete Record in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Get in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
- Delete Multiple Items in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
Tired of AWS Console? Try Dynobase.
First 7 days are on us. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase