Batch Insert in DynamoDB Using Java (Guide w/ Code Examples)
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!"); } }
Similar Code Examples
- Conditional Update in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Update Attribute in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Delete Multiple Items in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Query Local Secondary Index in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Insert in DynamoDB Using Java
- Update Multiple Items in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Query in DynamoDB Using Java
Tired of AWS Console? Try Dynobase.
Try 7-day free trial. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase