Search in sources :

Example 1 with Expression

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

Example 2 with Expression

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

the class EnhancedScanRecordsWithExpression method scanUsingContains.

// snippet-end:[dynamodb.java2.mapping.scanEx.main]
// Scan table for records where title contains the word issues using the contains function
public static void scanUsingContains(DynamoDbClient ddb, String tableName, String indexName) {
    System.out.println("\n***********************************************************\n");
    System.out.print("Select items for " + tableName + " where title contains the word issues");
    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));
        AttributeValue attVal = AttributeValue.builder().s("issue").build();
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attVal);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#title", "title");
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("contains(#title, :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)

Example 3 with Expression

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

the class EnhancedQueryRecordsWithFilter method queryTableFilter.

// snippet-start:[dynamodb.java2.mapping.queryfilter.main]
public static void queryTableFilter(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        AttributeValue att = AttributeValue.builder().s("sblue@noserver.com").build();
        Map<String, AttributeValue> expressionValues = new HashMap<>();
        expressionValues.put(":value", att);
        Expression expression = Expression.builder().expression("email = :value").expressionValues(expressionValues).build();
        // Create a QueryConditional object that is used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id103").build());
        // Get items in the Customer table and write out the ID value.
        Iterator<Customer> results = mappedTable.query(r -> r.queryConditional(queryConditional).filterExpression(expression)).items().iterator();
        while (results.hasNext()) {
            Customer rec = results.next();
            System.out.println("The record id is " + rec.getId());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 4 with Expression

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

Aggregations

Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)4 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)3 ScanEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)2 HashMap (java.util.HashMap)1 QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)1 Region (software.amazon.awssdk.regions.Region)1 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)1