Search in sources :

Example 6 with DynamoDbEnhancedClient

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

the class EnhancedQueryRecordsWithFilter method main.

// Query the Customer table using a filter.
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();
    queryTableFilter(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 7 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 8 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 9 with DynamoDbEnhancedClient

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

the class Scenario method scanMovies.

// snippet-end:[dynamodb.java2.scenario.query.main]
// snippet-start:[dynamodb.java2.scenario.scan.main]
// Scan the table.
public static void scanMovies(DynamoDbClient ddb, String tableName) {
    System.out.println("******* Scanning all movies.\n");
    try {
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        DynamoDbTable<Movies> custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
        Iterator<Movies> results = custTable.scan().items().iterator();
        while (results.hasNext()) {
            Movies rec = results.next();
            System.out.println("The movie title is " + rec.getTitle());
            System.out.println("The movie year is " + rec.getYear());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 10 with DynamoDbEnhancedClient

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

the class DynamoDBService method setItem.

public void setItem(PopData pop) {
    // Create a DynamoDbEnhancedClient.
    DynamoDbClient ddb = getClient();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object.
        DynamoDbTable<Population> workTable = enhancedClient.table("Country", TableSchema.fromBean(Population.class));
        // Populate the table.
        Population record = new Population();
        String name = pop.getName();
        String code = pop.getCode();
        record.setId(name);
        record.setCode(code);
        record.set2010(pop.get2010());
        record.set2011(pop.get2011());
        record.set2012(pop.get2012());
        record.set2013(pop.get2013());
        record.set2014(pop.get2014());
        record.set2015(pop.get2015());
        record.set2016(pop.get2016());
        record.set2017(pop.get2017());
        record.set2018(pop.get2018());
        record.set2019(pop.get2019());
        // Put the customer data into a DynamoDB table.
        workTable.putItem(record);
        System.out.println("Added record " + recNum);
        recNum++;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) 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