Search in sources :

Example 6 with QuerySpec

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);
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Item(com.amazonaws.services.dynamodbv2.document.Item) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 7 with QuerySpec

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;
    }
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 8 with QuerySpec

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<>();
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 9 with QuerySpec

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;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 10 with QuerySpec

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);
}
Also used : ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) RangeKeyCondition(com.amazonaws.services.dynamodbv2.document.RangeKeyCondition) ArrayList(java.util.ArrayList) Upload(org.sagebionetworks.bridge.models.upload.Upload) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Item(com.amazonaws.services.dynamodbv2.document.Item) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) ResourceList(org.sagebionetworks.bridge.models.ResourceList) ArrayList(java.util.ArrayList) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Aggregations

QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)26 Item (com.amazonaws.services.dynamodbv2.document.Item)17 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)15 Table (com.amazonaws.services.dynamodbv2.document.Table)12 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)6 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)4 Index (com.amazonaws.services.dynamodbv2.document.Index)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 ItemCollection (com.amazonaws.services.dynamodbv2.document.ItemCollection)2 DDBQueryOp (io.nosqlbench.adapter.dynamodb.optypes.DDBQueryOp)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)2 Upload (org.sagebionetworks.bridge.models.upload.Upload)2 Test (org.testng.annotations.Test)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1