Search in sources :

Example 1 with QueryConditional

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

the class EnhancedQueryRecordsWithSortKey method queryTableSort.

// snippet-start:[dynamodb.java2.mapping.querykey.main]
public static void queryTableSort(DynamoDbEnhancedClient enhancedClient) {
    try {
        // Create a DynamoDbTable object based on Issues.
        DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
        String dateVal = "2013-11-19";
        DynamoDbIndex<Issues> secIndex = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class)).index("dueDateIndex");
        AttributeValue attVal = AttributeValue.builder().s(dateVal).build();
        // Create a QueryConditional object that's used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(attVal).build());
        // Get items in the Issues table.
        SdkIterable<Page<Issues>> results = secIndex.query(QueryEnhancedRequest.builder().queryConditional(queryConditional).build());
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.set(0);
        results.forEach(page -> {
            Issues issue = (Issues) page.items().get(atomicInteger.get());
            System.out.println("The issue title is " + issue.getTitle());
            atomicInteger.incrementAndGet();
        });
    } 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) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Page(software.amazon.awssdk.enhanced.dynamodb.model.Page)

Example 2 with QueryConditional

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

the class EnhancedScanRecordsWithExpression method queryIndex.

// Query the table using a secondary index.
public static void queryIndex(DynamoDbClient ddb, String tableName, String indexName) {
    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));
        String dateVal = "2013-11-19";
        DynamoDbIndex<Issues> secIndex = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class)).index("dueDateIndex");
        AttributeValue attVal = AttributeValue.builder().s(dateVal).build();
        // Create a QueryConditional object that's used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(attVal).build());
        // Get items in the Issues table.
        SdkIterable<Page<Issues>> results = secIndex.query(QueryEnhancedRequest.builder().queryConditional(queryConditional).build());
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.set(0);
        results.forEach(page -> {
            Issues issue = (Issues) page.items().get(atomicInteger.get());
            System.out.println("The issue title is " + issue.getTitle());
            atomicInteger.incrementAndGet();
        });
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Page(software.amazon.awssdk.enhanced.dynamodb.model.Page) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 3 with QueryConditional

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

the class EnhancedQueryRecords method queryTable.

// snippet-start:[dynamodb.java2.mapping.query.main]
public static String queryTable(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id120").build());
        // Get items in the table and write out the ID value
        Iterator<Customer> results = mappedTable.query(queryConditional).items().iterator();
        String result = "";
        while (results.hasNext()) {
            Customer rec = results.next();
            result = rec.getId();
            System.out.println("The record id is " + result);
        }
        return result;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 4 with QueryConditional

use of software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional 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 5 with QueryConditional

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

the class Scenario method queryTable.

// snippet-end:[dynamodb.java2.scenario.create_table.main]
// snippet-start:[dynamodb.java2.scenario.query.main]
// Query the table.
public static void queryTable(DynamoDbClient ddb) {
    try {
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        DynamoDbTable<Movies> custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(2013).build());
        // Get items in the table and write out the ID value
        Iterator<Movies> results = custTable.query(queryConditional).items().iterator();
        String result = "";
        while (results.hasNext()) {
            Movies rec = results.next();
            System.out.println("The title of the movie is " + rec.getTitle());
            System.out.println("The movie information  is " + rec.getInfo());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Aggregations

QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)5 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)2 Page (software.amazon.awssdk.enhanced.dynamodb.model.Page)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 HashMap (java.util.HashMap)1 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)1