Insert in DynamoDB Using Java (Guide w/ Code Examples)
Provided by Rafal Wilinski
You can use the PutItemRequest
class offered by the DynamoDB SDK to put new items into DynamoDB.
The following code inserts an item into the table my-table
with three attributes: a primary key attribute ("id") and two other attributes ("attribute1" and "attribute2"). The values for these attributes are specified in the AttributeValue objects
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.AttributeValue.*; import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; import java.util.HashMap; import java.util.Map; public class InsertItem { public static void main(String[] args) { DynamoDbClient ddb = DynamoDbClient.create(); String tableName = "my-table"; Map<String, AttributeValue> item = new HashMap<>(); item.put("id", AttributeValue.builder().s("12345").build()); item.put("attribute1", AttributeValue.builder().s("value1").build()); item.put("attribute2", AttributeValue.builder().s("value2").build()); PutItemRequest request = PutItemRequest.builder() .tableName(tableName) .item(item) .build(); ddb.putItem(request); } }
Similar Code Examples
- Delete All Items in DynamoDB Using Java
- Update Multiple Attributes in DynamoDB Using Java
- Batch Put Item in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Delete Expression in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Update Attribute in DynamoDB Using Java
- Update Multiple Items in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Conditional Update in DynamoDB Using Java
- Delete Table in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Query Local Secondary Index in DynamoDB Using Java
Spend less time in the AWS console, use Dynobase.
Try 7-day free trial. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase