Search in sources :

Example 16 with QuerySpec

use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project athenz by AthenZ.

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 17 with QuerySpec

use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project Fidelius by FINRAOS.

the class JCredStash method getHighestVersion.

protected int getHighestVersion(String name, String tableName) {
    DynamoDB dynamoDB = new DynamoDB(amazonDynamoDBClient);
    Table table = dynamoDB.getTable(tableName);
    QuerySpec spec = new QuerySpec().withScanIndexForward(false).withConsistentRead(true).withKeyConditionExpression("#n = :v_name").withValueMap(new ValueMap().withString(":v_name", name)).withNameMap(new NameMap().with("#n", "name")).withProjectionExpression("version");
    ItemCollection<QueryOutcome> items = table.query(spec);
    Integer maxVersion = 0;
    Iterator<Item> iter = items.iterator();
    while (iter.hasNext()) {
        Item next = iter.next();
        Integer version = new Integer((String) next.get("version"));
        if (version.compareTo(maxVersion) > 0) {
            maxVersion = version.intValue();
        }
    }
    return maxVersion;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 18 with QuerySpec

use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIGlobalSecondaryIndexExample method queryIndex.

public static void queryIndex(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.print("Querying index " + indexName + "...");
    Index index = table.getIndex(indexName);
    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();
    if (indexName == "CreateDateIndex") {
        System.out.println("Issues filed on 2013-11-01");
        querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "TitleIndex") {
        System.out.println("Compilation errors");
        querySpec.withKeyConditionExpression("Title = :v_title and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_title", "Compilation error").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "DueDateIndex") {
        System.out.println("Items that are due on 2013-11-30");
        querySpec.withKeyConditionExpression("DueDate = :v_date").withValueMap(new ValueMap().withString(":v_date", "2013-11-30"));
        items = index.query(querySpec);
    } else {
        System.out.println("\nNo valid index name provided");
        return;
    }
    Iterator<Item> iterator = items.iterator();
    System.out.println("Query: printing results...");
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Index(com.amazonaws.services.dynamodbv2.document.Index) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 19 with QuerySpec

use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPILocalSecondaryIndexExample method query.

public static void query(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.println("Querying table " + tableName + "...");
    QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
    if (indexName == "IsOpenIndex") {
        System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open.");
        System.out.println("Only a user-specified list of attributes are returned\n");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_isopen", 1));
        querySpec.withProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else if (indexName == "OrderCreationDateIndex") {
        System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2015.");
        System.out.println("Only the projected attributes are returned\n");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and OrderCreationDate >= :v_orddate").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_orddate", 20150131));
        querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else {
        System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
        querySpec.withKeyConditionExpression("CustomerId = :v_custid").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com"));
        ItemCollection<QueryOutcome> items = table.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Iterator(java.util.Iterator) LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) Index(com.amazonaws.services.dynamodbv2.document.Index) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) ItemCollection(com.amazonaws.services.dynamodbv2.document.ItemCollection) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 20 with QuerySpec

use of com.amazonaws.services.dynamodbv2.document.spec.QuerySpec in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIQuery method findRepliesPostedWithinTimePeriod.

private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) {
    Table table = dynamoDB.getTable(tableName);
    long startDateMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
    long endDateMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L);
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String startDate = df.format(startDateMilli);
    String endDate = df.format(endDateMilli);
    String replyId = forumName + "#" + threadSubject;
    QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy").withKeyConditionExpression("Id = :v_id and ReplyDateTime between :v_start_dt and :v_end_dt").withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_start_dt", startDate).withString(":v_end_dt", endDate));
    ItemCollection<QueryOutcome> items = table.query(spec);
    System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) SimpleDateFormat(java.text.SimpleDateFormat) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Date(java.util.Date) Item(com.amazonaws.services.dynamodbv2.document.Item) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) SimpleDateFormat(java.text.SimpleDateFormat)

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