Search in sources :

Example 21 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-sdk-android by aws-amplify.

the class DynamoDBMapper method query.

/**
 * Queries an Amazon DynamoDB table and returns the matching results as an
 * unmodifiable list of instantiated objects. The table to query is
 * determined by looking at the annotations on the specified class, which
 * declares where to store the object data in Amazon DynamoDB, and the query
 * expression parameter allows the caller to filter results and control how
 * the query is executed.
 * <p>
 * When the query is on any local/global secondary index, callers should be
 * aware that the returned object(s) will only contain item attributes that
 * are projected into the index. All the other unprojected attributes will
 * be saved as type default values.
 * <p>
 * Callers should also be aware that the returned list is unmodifiable, and
 * any attempts to modify the list will result in an
 * UnsupportedOperationException.
 * <p>
 * You can specify the pagination loading strategy for this query operation.
 * By default, the list returned is lazily loaded when possible.
 *
 * @param <T> The type of the objects being returned.
 * @param clazz The class annotated with DynamoDB annotations describing how
 *            to store the object data in Amazon DynamoDB.
 * @param queryExpression Details on how to run the query, including any
 *            conditions on the key values
 * @param config The configuration to use for this query, which overrides
 *            the default provided at object construction.
 * @return An unmodifiable list of the objects constructed from the results
 *         of the query operation.
 * @see PaginatedQueryList
 * @see PaginationLoadingStrategy
 */
public <T> PaginatedQueryList<T> query(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) {
    config = mergeConfig(config);
    final QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config);
    final QueryResult queryResult = db.query(applyUserAgent(queryRequest));
    return new PaginatedQueryList<T>(this, clazz, db, queryRequest, queryResult, config.getPaginationLoadingStrategy(), config);
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest)

Example 22 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-athena-query-federation by awslabs.

the class DynamoDBRecordHandler method getIterator.

/*
    Creates an iterator that can iterate through a Query or Scan, sending paginated requests as necessary
     */
private Iterator<Map<String, AttributeValue>> getIterator(Split split, String tableName, Schema schema) {
    AmazonWebServiceRequest request = buildReadRequest(split, tableName, schema);
    return new Iterator<Map<String, AttributeValue>>() {

        AtomicReference<Map<String, AttributeValue>> lastKeyEvaluated = new AtomicReference<>();

        AtomicReference<Iterator<Map<String, AttributeValue>>> currentPageIterator = new AtomicReference<>();

        @Override
        public boolean hasNext() {
            return currentPageIterator.get() == null || currentPageIterator.get().hasNext() || lastKeyEvaluated.get() != null;
        }

        @Override
        public Map<String, AttributeValue> next() {
            if (currentPageIterator.get() != null && currentPageIterator.get().hasNext()) {
                return currentPageIterator.get().next();
            }
            Iterator<Map<String, AttributeValue>> iterator;
            try {
                if (request instanceof QueryRequest) {
                    QueryRequest paginatedRequest = ((QueryRequest) request).withExclusiveStartKey(lastKeyEvaluated.get());
                    logger.info("Invoking DDB with Query request: {}", request);
                    QueryResult queryResult = invokerCache.get(tableName).invoke(() -> ddbClient.query(paginatedRequest));
                    lastKeyEvaluated.set(queryResult.getLastEvaluatedKey());
                    iterator = queryResult.getItems().iterator();
                } else {
                    ScanRequest paginatedRequest = ((ScanRequest) request).withExclusiveStartKey(lastKeyEvaluated.get());
                    logger.info("Invoking DDB with Scan request: {}", request);
                    ScanResult scanResult = invokerCache.get(tableName).invoke(() -> ddbClient.scan(paginatedRequest));
                    lastKeyEvaluated.set(scanResult.getLastEvaluatedKey());
                    iterator = scanResult.getItems().iterator();
                }
            } catch (TimeoutException | ExecutionException e) {
                throw new RuntimeException(e);
            }
            currentPageIterator.set(iterator);
            if (iterator.hasNext()) {
                return iterator.next();
            } else {
                return null;
            }
        }
    };
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) Iterator(java.util.Iterator) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)21 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)19 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 HashMap (java.util.HashMap)9 Condition (com.amazonaws.services.dynamodbv2.model.Condition)8 Map (java.util.Map)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 AmazonClientException (com.amazonaws.AmazonClientException)2 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)2 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)2 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 Document (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)1 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)1 ComparisonOperator (com.amazonaws.services.dynamodbv2.model.ComparisonOperator)1 ImmutableMap (com.google.common.collect.ImmutableMap)1