Search in sources :

Example 16 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project jcabi-dynamo by jcabi.

the class QueryValve method count.

@Override
public int count(final Credentials credentials, final String table, final Map<String, Condition> conditions) throws IOException {
    final AmazonDynamoDB aws = credentials.aws();
    try {
        QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withSelect(Select.COUNT).withLimit(Integer.MAX_VALUE);
        if (!this.index.isEmpty()) {
            request = request.withIndexName(this.index);
        }
        final long start = System.currentTimeMillis();
        final QueryResult rslt = aws.query(request);
        final int count = rslt.getCount();
        Logger.info(this, // @checkstyle LineLength (1 line)
        "#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s", count, request.getTableName(), request.getQueryFilter(), new PrintableConsumedCapacity(rslt.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return count;
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to count from \"%s\" by %s", table, conditions), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 17 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project geowave by locationtech.

the class DynamoDBMetadataDeleter method delete.

@Override
public boolean delete(final MetadataQuery metadata) {
    // the nature of metadata deleter is that primary ID is always
    // well-defined and it is deleting a single entry at a time
    final String tableName = operations.getMetadataTableName(metadataType);
    if (!metadata.hasPrimaryId() && !metadata.hasSecondaryId()) {
        if (operations.getOptions().getBaseOptions().isVisibilityEnabled()) {
            // we need to respect visibilities although this may be much slower
            DataStoreUtils.safeMetadataDelete(this, operations, metadataType, metadata);
        } else {
            // without visibilities it is much faster to drop the table
            operations.dropMetadataTable(metadataType);
        }
        return true;
    }
    final QueryRequest queryRequest = new QueryRequest(tableName);
    if (metadata.hasSecondaryId()) {
        queryRequest.withFilterExpression(DynamoDBOperations.METADATA_SECONDARY_ID_KEY + " = :secVal").addExpressionAttributeValuesEntry(":secVal", new AttributeValue().withB(ByteBuffer.wrap(metadata.getSecondaryId())));
    }
    if (metadata.hasPrimaryId()) {
        queryRequest.withKeyConditionExpression(DynamoDBOperations.METADATA_PRIMARY_ID_KEY + " = :priVal").addExpressionAttributeValuesEntry(":priVal", new AttributeValue().withB(ByteBuffer.wrap(metadata.getPrimaryId())));
    }
    final QueryResult queryResult = operations.getClient().query(queryRequest);
    for (final Map<String, AttributeValue> entry : queryResult.getItems()) {
        final Map<String, AttributeValue> key = new HashMap<>();
        key.put(DynamoDBOperations.METADATA_PRIMARY_ID_KEY, entry.get(DynamoDBOperations.METADATA_PRIMARY_ID_KEY));
        key.put(DynamoDBOperations.METADATA_TIMESTAMP_KEY, entry.get(DynamoDBOperations.METADATA_TIMESTAMP_KEY));
        operations.getClient().deleteItem(tableName, key);
    }
    return true;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap)

Example 18 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project geowave by locationtech.

the class DynamoDBMetadataReader method query.

@Override
public CloseableIterator<GeoWaveMetadata> query(final MetadataQuery query) {
    final String tableName = operations.getMetadataTableName(metadataType);
    final boolean needsVisibility = metadataType.isStatValues() && operations.getOptions().getBaseOptions().isVisibilityEnabled();
    final Iterator<Map<String, AttributeValue>> iterator;
    if (!query.hasPrimaryIdRanges()) {
        if (query.hasPrimaryId() && query.isExact()) {
            final QueryRequest queryRequest = new QueryRequest(tableName);
            if (query.hasSecondaryId()) {
                queryRequest.withFilterExpression(DynamoDBOperations.METADATA_SECONDARY_ID_KEY + " = :secVal").addExpressionAttributeValuesEntry(":secVal", new AttributeValue().withB(ByteBuffer.wrap(query.getSecondaryId())));
            }
            queryRequest.withKeyConditionExpression(DynamoDBOperations.METADATA_PRIMARY_ID_KEY + " = :priVal").addExpressionAttributeValuesEntry(":priVal", new AttributeValue().withB(ByteBuffer.wrap(query.getPrimaryId())));
            final QueryResult queryResult = operations.getClient().query(queryRequest);
            return wrapIterator(queryResult.getItems().iterator(), query, needsVisibility);
        }
        final ScanRequest scan = new ScanRequest(tableName);
        if (query.hasPrimaryId()) {
            scan.addScanFilterEntry(DynamoDBOperations.METADATA_PRIMARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(query.getPrimaryId()))).withComparisonOperator(ComparisonOperator.BEGINS_WITH));
        }
        if (query.hasSecondaryId()) {
            scan.addScanFilterEntry(DynamoDBOperations.METADATA_SECONDARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(query.getSecondaryId()))).withComparisonOperator(ComparisonOperator.EQ));
        }
        final ScanResult scanResult = operations.getClient().scan(scan);
        iterator = new LazyPaginatedScan(scanResult, scan, operations.getClient());
    } else {
        iterator = Iterators.concat(Arrays.stream(query.getPrimaryIdRanges()).map(r -> {
            final ScanRequest scan = new ScanRequest(tableName);
            if (query.hasSecondaryId()) {
                scan.addScanFilterEntry(DynamoDBOperations.METADATA_SECONDARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(query.getSecondaryId()))).withComparisonOperator(ComparisonOperator.EQ));
            }
            if (r.getStart() != null) {
                if (r.getEnd() != null) {
                    scan.addScanFilterEntry(DynamoDBOperations.METADATA_PRIMARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(r.getStart())), new AttributeValue().withB(ByteBuffer.wrap(ByteArrayUtils.getNextInclusive(r.getEnd())))).withComparisonOperator(ComparisonOperator.BETWEEN));
                } else {
                    scan.addScanFilterEntry(DynamoDBOperations.METADATA_PRIMARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(r.getStart()))).withComparisonOperator(ComparisonOperator.GE));
                }
            } else if (r.getEnd() != null) {
                scan.addScanFilterEntry(DynamoDBOperations.METADATA_PRIMARY_ID_KEY, new Condition().withAttributeValueList(new AttributeValue().withB(ByteBuffer.wrap(r.getEndAsNextPrefix()))).withComparisonOperator(ComparisonOperator.LT));
            }
            final ScanResult scanResult = operations.getClient().scan(scan);
            return new LazyPaginatedScan(scanResult, scan, operations.getClient());
        }).iterator());
    }
    return wrapIterator(iterator, query, needsVisibility);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Condition(com.amazonaws.services.dynamodbv2.model.Condition) Arrays(java.util.Arrays) NoopClosableIteratorWrapper(org.locationtech.geowave.datastore.dynamodb.util.DynamoDBUtils.NoopClosableIteratorWrapper) DynamoDBUtils(org.locationtech.geowave.datastore.dynamodb.util.DynamoDBUtils) Iterator(java.util.Iterator) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) MetadataReader(org.locationtech.geowave.core.store.operations.MetadataReader) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) ByteBuffer(java.nio.ByteBuffer) MetadataQuery(org.locationtech.geowave.core.store.operations.MetadataQuery) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) Iterators(com.google.common.collect.Iterators) LazyPaginatedScan(org.locationtech.geowave.datastore.dynamodb.util.LazyPaginatedScan) ComparisonOperator(com.amazonaws.services.dynamodbv2.model.ComparisonOperator) MetadataType(org.locationtech.geowave.core.store.operations.MetadataType) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Map(java.util.Map) ByteArrayUtils(org.locationtech.geowave.core.index.ByteArrayUtils) MetadataIterators(org.locationtech.geowave.core.store.metadata.MetadataIterators) GeoWaveMetadata(org.locationtech.geowave.core.store.entities.GeoWaveMetadata) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) Map(java.util.Map) LazyPaginatedScan(org.locationtech.geowave.datastore.dynamodb.util.LazyPaginatedScan)

Example 19 with QueryResult

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

the class DynamoDBMapper method queryPage.

/**
 * Queries an Amazon DynamoDB table and returns a single page of matching
 * results. 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.
 *
 * @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 AWS 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 a single page of matching results
 */
public <T> QueryResultPage<T> queryPage(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) {
    config = mergeConfig(config);
    final QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config);
    final QueryResult scanResult = db.query(applyUserAgent(queryRequest));
    final QueryResultPage<T> result = new QueryResultPage<T>();
    final List<AttributeTransformer.Parameters<T>> parameters = toParameters(scanResult.getItems(), clazz, queryRequest.getTableName(), config);
    result.setResults(marshallIntoObjects(parameters));
    result.setLastEvaluatedKey(scanResult.getLastEvaluatedKey());
    return result;
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest)

Example 20 with QueryResult

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

the class DynamoDBMapper method count.

/**
 * Evaluates the specified query expression and returns the count of
 * matching items, without returning any of the actual item data.
 *
 * @param clazz The class mapped to a DynamoDB table.
 * @param queryExpression The parameters for running the scan.
 * @param config The mapper configuration to use for the query, which
 *            overrides the default provided at object construction.
 * @param <T> the type of the object.
 * @return The count of matching items, without returning any of the actual
 *         item data.
 */
public <T> int count(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) {
    config = mergeConfig(config);
    final QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config);
    queryRequest.setSelect(Select.COUNT);
    // Count queries can also be truncated for large datasets
    int count = 0;
    QueryResult queryResult = null;
    do {
        queryResult = db.query(applyUserAgent(queryRequest));
        count += queryResult.getCount();
        queryRequest.setExclusiveStartKey(queryResult.getLastEvaluatedKey());
    } while (queryResult.getLastEvaluatedKey() != null);
    return count;
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest)

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