Search in sources :

Example 1 with BatchWriteItemEnhancedRequest

use of software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest in project aws-doc-sdk-examples by awsdocs.

the class EnhancedBatchWriteItems method putBatchRecords.

// snippet-start:[dynamodb.java2.mapping.batchitems.main]
public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        Customer record2 = new Customer();
        record2.setCustName("Fred Pink");
        record2.setId("id110");
        record2.setEmail("fredp@noserver.com");
        record2.setRegistrationDate(instant);
        Customer record3 = new Customer();
        record3.setCustName("Susan Pink");
        record3.setId("id120");
        record3.setEmail("spink@noserver.com");
        record3.setRegistrationDate(instant);
        // Create a BatchWriteItemEnhancedRequest object
        BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder().writeBatches(WriteBatch.builder(Customer.class).mappedTableResource(mappedTable).addPutItem(r -> r.item(record2)).addPutItem(r -> r.item(record3)).build()).build();
        // Add these two items to the table
        enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
        System.out.println("done");
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) LocalDateTime(java.time.LocalDateTime) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) DynamoDbPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey) Instant(java.time.Instant) DynamoDbBean(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbSortKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey) WriteBatch(software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch) LocalDate(java.time.LocalDate) ZoneOffset(java.time.ZoneOffset) Region(software.amazon.awssdk.regions.Region) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) LocalDate(java.time.LocalDate)

Aggregations

Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)1 DynamoDbTable (software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable)1 TableSchema (software.amazon.awssdk.enhanced.dynamodb.TableSchema)1 DynamoDbBean (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean)1 DynamoDbPartitionKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)1 DynamoDbSortKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey)1 BatchWriteItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest)1 WriteBatch (software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch)1 Region (software.amazon.awssdk.regions.Region)1 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)1 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)1