Search in sources :

Example 1 with DynamoDbEnhancedClient

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

the class PersistCase method putRecord.

// Puts an item into a DynamoDB table
public void putRecord(String caseId, String employeeName, String email) {
    // Create a DynamoDbClient object
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object
        DynamoDbTable<Case> caseTable = enhancedClient.table("Case", TableSchema.fromBean(Case.class));
        // Create an Instant object
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        // Populate the table
        Case caseRecord = new Case();
        caseRecord.setName(employeeName);
        caseRecord.setId(caseId);
        caseRecord.setEmail(email);
        caseRecord.setRegistrationDate(instant);
        // Put the case data into a DynamoDB table
        caseTable.putItem(caseRecord);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("done");
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) Region(software.amazon.awssdk.regions.Region) LocalDate(java.time.LocalDate) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 2 with DynamoDbEnhancedClient

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

the class ScanEmployees method sendEmployeMessage.

public Boolean sendEmployeMessage() {
    Boolean send = false;
    String myDate = getDate();
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    // Create a DynamoDbTable object based on Employee.
    DynamoDbTable<Employee> table = enhancedClient.table("Employee", TableSchema.fromBean(Employee.class));
    try {
        AttributeValue attVal = AttributeValue.builder().s(myDate).build();
        // Get only items in the Employee table that match the date.
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attVal);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#startDate", "startDate");
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#startDate = :val1").build();
        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(// you can increase this value.
        15).build();
        // Get items in the Employee table.
        Iterator<Employee> employees = table.scan(enhancedRequest).items().iterator();
        while (employees.hasNext()) {
            Employee employee = employees.next();
            String first = employee.getFirst();
            String phone = employee.getPhone();
            // Send an anniversary message.
            sentTextMessage(first, phone);
            send = true;
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return send;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 3 with DynamoDbEnhancedClient

use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient 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)

Example 4 with DynamoDbEnhancedClient

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

the class EnhancedBatchWriteItems method main.

public static void main(String[] args) {
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    putBatchRecords(enhancedClient);
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 5 with DynamoDbEnhancedClient

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

the class EnhancedScanRecordsWithExpression method scanIndex.

// snippet-start:[dynamodb.java2.mapping.scanEx.main]
// Scan the table and retrieve only items where createDate is 2013-11-15.
public static void scanIndex(DynamoDbClient ddb, String tableName, String indexName) {
    System.out.println("\n***********************************************************\n");
    System.out.print("Select items for " + tableName + " where createDate is 2013-11-15!");
    try {
        // Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        // Create a DynamoDbTable object based on Issues.
        DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
        // Setup the scan based on the index.
        if (indexName == "CreateDateIndex") {
            System.out.println("Issues filed on 2013-11-15");
            AttributeValue attVal = AttributeValue.builder().s("2013-11-15").build();
            // Get only items in the Issues table for 2013-11-15.
            Map<String, AttributeValue> myMap = new HashMap<>();
            myMap.put(":val1", attVal);
            Map<String, String> myExMap = new HashMap<>();
            myExMap.put("#createDate", "createDate");
            Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#createDate = :val1").build();
            ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(15).build();
            // Get items in the Issues table.
            Iterator<Issues> results = table.scan(enhancedRequest).items().iterator();
            while (results.hasNext()) {
                Issues issue = results.next();
                System.out.println("The record description is " + issue.getDescription());
                System.out.println("The record title is " + issue.getTitle());
            }
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Aggregations

DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)21 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)14 Region (software.amazon.awssdk.regions.Region)12 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)8 Instant (java.time.Instant)3 LocalDateTime (java.time.LocalDateTime)3 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)3 ScanEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest)3 LocalDate (java.time.LocalDate)2 QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 File (java.io.File)1 ZoneOffset (java.time.ZoneOffset)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1