use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project aws-doc-sdk-examples by awsdocs.
the class TryDaxTests method queryTest.
void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) {
long startTime, endTime;
System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2);
Table table = client.getTable(tableName);
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":pkval", pk);
valueMap.put(":skval1", sk1);
valueMap.put(":skval2", sk2);
QuerySpec spec = new QuerySpec().withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2").withValueMap(valueMap);
for (int i = 0; i < iterations; i++) {
startTime = System.nanoTime();
ItemCollection<QueryOutcome> items = table.query(spec);
try {
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
iter.next();
}
} catch (Exception e) {
System.err.println("Unable to query table:");
e.printStackTrace();
}
endTime = System.nanoTime();
printTime(startTime, endTime, iterations);
}
}
use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnection method mostUpdatedHostRecord.
private boolean mostUpdatedHostRecord(Item recordToCheck) {
try {
// Query all records with the same hostName / provider / service as recordToCheck
QuerySpec spec = new QuerySpec().withKeyConditionExpression("hostName = :v_host_name").withFilterExpression("attribute_exists(provider) AND provider = :v_provider AND attribute_exists(service) AND service = :v_service").withValueMap(new ValueMap().withString(":v_host_name", recordToCheck.getString(KEY_HOSTNAME)).withString(":v_provider", recordToCheck.getString(KEY_PROVIDER)).withString(":v_service", recordToCheck.getString(KEY_SERVICE)));
ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> hostNameIndex.query(spec));
List<Item> allRecordsWithHost = new ArrayList<>();
for (Item item : outcome) {
allRecordsWithHost.add(item);
}
// Verify recordToCheck is the most updated record with this hostName
return dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(recordToCheck, allRecordsWithHost, KEY_CURRENT_TIME, KEY_PRIMARY);
} catch (Exception ex) {
LOGGER.error("DynamoDB mostUpdatedHostRecord failed for item: {}, error: {}", recordToCheck.toString(), ex.getMessage());
return false;
}
}
use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnection method getUnrefreshedCertRecordsByDate.
private List<Item> getUnrefreshedCertRecordsByDate(String provider, long yesterday, String unrefreshedCertDate) {
try {
QuerySpec spec = new QuerySpec().withKeyConditionExpression("currentDate = :v_current_date").withFilterExpression("provider = :v_provider AND attribute_exists(hostName) AND (attribute_not_exists(lastNotifiedTime) OR lastNotifiedTime < :v_last_notified)").withValueMap(new ValueMap().withString(":v_current_date", unrefreshedCertDate).withNumber(":v_last_notified", yesterday).withString(":v_provider", provider));
ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> currentTimeIndex.query(spec));
List<Item> items = new ArrayList<>();
for (Item item : outcome) {
items.add(item);
}
return items;
} catch (Exception ex) {
LOGGER.error("DynamoDB getUnrefreshedCertRecordsByDate failed for provider: {}, date: {} error: {}", provider, unrefreshedCertDate, ex.getMessage());
}
return new ArrayList<>();
}
use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project BridgeServer2 by Sage-Bionetworks.
the class DynamoScheduledActivityDao method query.
private List<DynamoScheduledActivity> query(String healthCode, String offsetKey, int querySize, RangeKeyCondition dateCondition) {
QuerySpec spec = new QuerySpec().withScanIndexForward(true).withHashKey(HEALTH_CODE, healthCode).withMaxPageSize(querySize).withRangeKeyCondition(dateCondition);
QueryOutcome outcome = referentIndex.query(spec);
List<DynamoScheduledActivity> itemsToLoad = Lists.newArrayList();
for (Item item : outcome.getItems()) {
DynamoScheduledActivity keys = new DynamoScheduledActivity();
keys.setGuid(item.getString(GUID));
keys.setReferentGuid(item.getString(REFERENT_GUID));
keys.setHealthCode(item.getString(HEALTH_CODE));
itemsToLoad.add(keys);
}
itemsToLoad.sort(ScheduledActivity::compareByReferentGuidThenGuid);
return itemsToLoad;
}
use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project BridgeServer2 by Sage-Bionetworks.
the class DynamoUploadDao method getUploads.
/**
* {@inheritDoc}
*/
@Override
public ForwardCursorPagedResourceList<Upload> getUploads(String healthCode, DateTime startTime, DateTime endTime, int pageSize, String offsetKey) {
// Set a sane upper limit on this.
if (pageSize < 1 || pageSize > API_MAXIMUM_PAGE_SIZE) {
throw new BadRequestException(PAGE_SIZE_ERROR);
}
int sizeWithIndicatorRecord = pageSize + 1;
long offsetTimestamp = (offsetKey == null) ? 0 : Long.parseLong(offsetKey);
long offsetStartTime = Math.max(startTime.getMillis(), offsetTimestamp);
RangeKeyCondition condition = new RangeKeyCondition(REQUESTED_ON).between(offsetStartTime, endTime.getMillis());
QuerySpec spec = new QuerySpec().withHashKey(HEALTH_CODE, healthCode).withMaxPageSize(sizeWithIndicatorRecord).withRangeKeyCondition(// this is not a filter, it should not require paging on our side.
condition);
QueryOutcome outcome = healthCodeRequestedOnIndex.query(spec);
List<Upload> itemsToLoad = new ArrayList<>(sizeWithIndicatorRecord);
Iterator<Item> iter = outcome.getItems().iterator();
while (iter.hasNext() && itemsToLoad.size() < sizeWithIndicatorRecord) {
Item item = iter.next();
DynamoUpload2 indexKeys = BridgeObjectMapper.get().convertValue(item.asMap(), DynamoUpload2.class);
itemsToLoad.add(indexKeys);
}
List<Upload> results = new ArrayList<>(sizeWithIndicatorRecord);
Map<String, List<Object>> resultMap = mapper.batchLoad(itemsToLoad);
for (List<Object> resultList : resultMap.values()) {
for (Object oneResult : resultList) {
results.add((Upload) oneResult);
}
}
// Due to the return in a map, these items are not in order by requestedOn attribute, so sort them.
results.sort(Comparator.comparing(Upload::getRequestedOn));
String nextOffsetKey = null;
if (results.size() > pageSize) {
nextOffsetKey = Long.toString(Iterables.getLast(results).getRequestedOn());
}
int lastIndex = Math.min(pageSize, results.size());
return new ForwardCursorPagedResourceList<>(results.subList(0, lastIndex), nextOffsetKey).withRequestParam(ResourceList.OFFSET_KEY, offsetKey).withRequestParam(ResourceList.PAGE_SIZE, pageSize).withRequestParam(ResourceList.START_TIME, startTime).withRequestParam(ResourceList.END_TIME, endTime);
}
Aggregations