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 Index in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Query Date Range in DynamoDB Using Java
- Query Hash Key Only in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Batch Load Using DynamoDB Mapper
- Update Item in DynamoDB Using Java
- Query Local Secondary Index in DynamoDB Using Java
- Batch Query in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Batch Write Using DynamoDB Mapper
- Remove Attribute in DynamoDB Using Java
Better DynamoDB experience.
First 7 days are on us. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase