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();
}
}
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;
}
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);
}
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;
}
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;
}
Aggregations