Example usage for com.amazonaws.services.dynamodbv2.document Index query

List of usage examples for com.amazonaws.services.dynamodbv2.document Index query

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Index query.

Prototype

@Override
    public ItemCollection<QueryOutcome> query(KeyAttribute hashKey) 

Source Link

Usage

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDocumentStoreTemplate.java

License:Apache License

private <T extends Item> Collection<T> executeQuery(final AttributeQuery query, final Class<T> itemClass) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();

    final Table table = dynamoDBClient.getTable(tableName);

    final List<T> totalItems = new ArrayList<>();

    if (itemConfiguration.hasIndexOn(query.getAttributeName())
            && query.getCondition().getComparisonOperator() == Operators.EQUALS) {

        final QuerySpec querySpec = generateQuerySpec(query);
        final ItemCollection<QueryOutcome> queryOutcome;

        if (itemConfiguration.primaryKeyDefinition().propertyName().equals(query.getAttributeName())) {
            // if the query is for the has then call query on table
            queryOutcome = table.query(querySpec);
        } else {/*  ww w . j a v a 2  s  .c  o m*/
            final Index index = table.getIndex(query.getAttributeName() + "_idx");
            queryOutcome = index.query(querySpec);
        }

        final Iterator<com.amazonaws.services.dynamodbv2.document.Item> iterator = queryOutcome.iterator();
        while (iterator != null && iterator.hasNext()) {
            final com.amazonaws.services.dynamodbv2.document.Item item = iterator.next();
            totalItems.add(stringToItem(item.toJSON(), itemClass));
        }
    } else {
        logger.debug("Performing table scan with query: " + query);
        ScanSpec scanSpec = null;
        try {
            scanSpec = generateScanSpec(query, itemClass);
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            throw new PersistenceResourceFailureException("Could not create ScanSpec for query: " + query, e);
        }
        final ItemCollection<ScanOutcome> scanOutcome = table.scan(scanSpec);

        final Iterator<com.amazonaws.services.dynamodbv2.document.Item> iterator = scanOutcome.iterator();
        while (iterator.hasNext()) {
            final com.amazonaws.services.dynamodbv2.document.Item item = iterator.next();
            totalItems.add(stringToItem(item.toJSON(), itemClass));
        }
    }

    return totalItems;
}

From source file:com.erudika.para.persistence.AWSDynamoDAO.java

License:Apache License

private <P extends ParaObject> String readPageFromSharedTable(String appid, Pager pager,
        LinkedList<P> results) {
    String lastKeyFragment = "";
    ValueMap valueMap = new ValueMap().withString(":aid", appid);
    NameMap nameMap = null;/*from   w  ww. j  a v a2  s . co  m*/

    if (!StringUtils.isBlank(pager.getLastKey())) {
        lastKeyFragment = " and #stamp > :ts";
        valueMap.put(":ts", pager.getLastKey());
        nameMap = new NameMap().with("#stamp", Config._TIMESTAMP);
    }

    Index index = getSharedIndex();
    QuerySpec spec = new QuerySpec().withMaxPageSize(pager.getLimit()).withMaxResultSize(pager.getLimit())
            .withKeyConditionExpression(Config._APPID + " = :aid" + lastKeyFragment).withValueMap(valueMap)
            .withNameMap(nameMap);

    if (index != null) {
        Page<Item, QueryOutcome> items = index.query(spec).firstPage();
        for (Item item : items) {
            P obj = ParaObjectUtils.setAnnotatedFields(item.asMap());
            if (obj != null) {
                results.add(obj);
            }
        }
    }

    if (!results.isEmpty()) {
        return Long.toString(results.peekLast().getTimestamp());
    } else {
        return null;
    }
}

From source file:com.exorath.service.party.service.DynamoDatabaseProvider.java

License:Apache License

/**
 * Get all parties where the key 'primKey' is 'uuid'
 * Some parties may be expired, service should check for those!
 *
 * @param uuid    The uuid to be looked for
 * @param primKey The index to search for the data
 * @return A list of all parties that matched the uuid to the primary key
 *//*from  ww w.ja v  a2s. c  o  m*/
private List<Party> getParties(String uuid, String primKey) {
    QuerySpec query = getQuerySpec(primKey, uuid);
    ArrayList<Item> items = new ArrayList<>();

    ItemCollection<QueryOutcome> queryOutcome;
    if (primKey.equals(PARTY_UUID)) {
        queryOutcome = table.query(query);
    } else {
        Index index = table.getIndex(primKey);
        queryOutcome = index.query(query);
    }
    for (Page<Item, QueryOutcome> page : queryOutcome.pages()) {
        for (Item aPage : page) {
            items.add(aPage);
        }
    }

    if (items.size() > 0) { // Did the query return something?
        List<Party> parties = new ArrayList<>();
        for (Item item : items) {
            parties.add(getPartyFromItem(item));
        }
        return parties;
    }
    return null; // The party must not exist
}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static ArrayList<String> listOpenExecutionsFromDynamo(String config) {
    ArrayList<String> retValue = new ArrayList<String>();

    DynamoDB dynamoDB = getDynamoDB();/*  w ww. ja v a2  s. c om*/
    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    querySpec.withKeyConditionExpression("loopState = :v_state")
            .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));

    items = index.query(querySpec);

    Iterator<Item> iterator = items.iterator();
    int totalActualCount = 0;
    Item item;

    while (iterator.hasNext()) {

        item = iterator.next();

        retValue.add(item.getString("clientId"));

    }

    return retValue;

}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static int countActiveJobs() {
    DynamoDB dynamoDB = getDynamoDB();/*from   w w  w . j  a  v  a  2 s. c  o m*/

    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    int count = 0;

    querySpec.withKeyConditionExpression("loopState = :v_state")
            .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));
    items = index.query(querySpec);
    Iterator<Item> iterator = items.iterator();

    while (iterator.hasNext()) {

        iterator.next();
        count++;
    }

    return count;

}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static void listActiveJobs(String optionalExecutionId) {
    DynamoDB dynamoDB = getDynamoDB();//w w  w  .  j a va  2  s. c om

    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    if ((optionalExecutionId != null) && !optionalExecutionId.isEmpty()) {
        querySpec.withKeyConditionExpression("loopState = :v_state and begins_with(clientId, :v_eid)")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING").withString(":v_eid",
                        optionalExecutionId));

    } else {

        querySpec.withKeyConditionExpression("loopState = :v_state")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));
    }

    items = index.query(querySpec);
    Iterator<Item> iterator = items.iterator();

    System.out.format("%20s %7s %28s %40s\n", "CronExpression", "Loop Count", "Next Scheduled Time       ",
            " ExecutionId");
    while (iterator.hasNext()) {

        Item item = iterator.next();

        System.out.format("%20s %7s %28s %40s\n", item.get("cronExpression"), item.get("loopCount"),
                item.get("lastProposedTimeDate"), item.get("clientId"));

    }

}