Search in sources :

Example 1 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project camel by apache.

the class SelectCommand method execute.

public void execute() {
    SelectRequest request = new SelectRequest().withSelectExpression(determineSelectExpression()).withConsistentRead(determineConsistentRead()).withNextToken(determineNextToken());
    log.trace("Sending request [{}] for exchange [{}]...", request, exchange);
    SelectResult result = this.sdbClient.select(request);
    log.trace("Received result [{}]", result);
    Message msg = getMessageForResponse(exchange);
    msg.setHeader(SdbConstants.ITEMS, result.getItems());
    msg.setHeader(SdbConstants.NEXT_TOKEN, result.getNextToken());
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Message(org.apache.camel.Message) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 2 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project SimianArmy by Netflix.

the class SimpleDBRecorder method findEvents.

/**
     * Find events.
     *
     * @param queryMap
     *            the query map
     * @param after
     *            the start time to query for all events after
     * @return the list
     */
protected List<Event> findEvents(Map<String, String> queryMap, long after) {
    StringBuilder query = new StringBuilder(String.format("select * from `%s` where region = '%s'", domain, region));
    for (Map.Entry<String, String> pair : queryMap.entrySet()) {
        query.append(String.format(" and %s = '%s'", pair.getKey(), pair.getValue()));
    }
    query.append(String.format(" and eventTime > '%d'", after));
    // always return with most recent record first
    query.append(" order by eventTime desc");
    List<Event> list = new LinkedList<Event>();
    SelectRequest request = new SelectRequest(query.toString());
    request.setConsistentRead(Boolean.TRUE);
    SelectResult result = new SelectResult();
    do {
        result = sdbClient().select(request.withNextToken(result.getNextToken()));
        for (Item item : result.getItems()) {
            Map<String, String> fields = new HashMap<String, String>();
            Map<String, String> res = new HashMap<String, String>();
            for (Attribute attr : item.getAttributes()) {
                if (Keys.KEYSET.contains(attr.getName())) {
                    res.put(attr.getName(), attr.getValue());
                } else {
                    fields.put(attr.getName(), attr.getValue());
                }
            }
            String eid = res.get(Keys.id.name());
            String ereg = res.get(Keys.region.name());
            MonkeyType monkeyType = valueToEnum(MonkeyType.class, res.get(Keys.monkeyType.name()));
            EventType eventType = valueToEnum(EventType.class, res.get(Keys.eventType.name()));
            long eventTime = Long.parseLong(res.get(Keys.eventTime.name()));
            list.add(new BasicRecorderEvent(monkeyType, eventType, ereg, eid, eventTime).addFields(fields));
        }
    } while (result.getNextToken() != null);
    return list;
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) LinkedList(java.util.LinkedList) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 3 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project siena by mandubian.

the class SdbPersistenceManager method rawGet.

protected <T> int rawGet(Iterable<T> models) {
    StringBuffer domainBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildBatchGetQuery(models, prefix, domainBuf);
    req.setConsistentRead(isReadConsistent());
    try {
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);
        int nb = SdbMappingUtils.mapSelectResult(res, models);
        // join management
        // gets class
        Class<?> clazz = null;
        for (T obj : models) {
            if (clazz == null) {
                clazz = obj.getClass();
                break;
            }
        }
        if (!ClassInfo.getClassInfo(clazz).joinFields.isEmpty()) {
            mapJoins(models);
        }
        return nb;
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) AmazonClientException(com.amazonaws.AmazonClientException) SienaException(siena.SienaException) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 4 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project siena by mandubian.

the class SdbPersistenceManager method doFetchList.

protected <T> void doFetchList(Query<T> query, int limit, int offset, List<T> resList, int depth) {
    if (depth >= MAX_DEPTH) {
        throw new SienaException("Reached maximum depth of recursion when retrieving more data (" + MAX_DEPTH + ")");
    }
    preFetch(query, limit, offset, !resList.isEmpty());
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return;
    }
    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);
        // activates the SdbCtx now that it is really initialised
        sdbCtx.activate();
        postFetch(query, res);
        // cursor not yet created
        switch(fetchType.fetchType) {
            case KEYS_ONLY:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
                }
                break;
            case NORMAL:
            default:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
                }
                // join management
                if (!query.getJoins().isEmpty() || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                    mapJoins(query, resList);
        }
        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    } else {
        // we prepare the query each time
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture			
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = sdb.select(req);
        postFetch(query, res);
        switch(fetchType.fetchType) {
            case KEYS_ONLY:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
                }
                break;
            case NORMAL:
            default:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
                }
                // join management
                if (!query.getJoins().isEmpty() || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                    mapJoins(query, resList);
        }
        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    }
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) SienaException(siena.SienaException) QueryOptionFetchType(siena.core.options.QueryOptionFetchType) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 5 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project siena by mandubian.

the class SdbPersistenceManager method doFetchIterable.

protected <T> Iterable<T> doFetchIterable(Query<T> query, int limit, int offset, boolean recursing) {
    preFetch(query, limit, offset, recursing);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return new ArrayList<T>();
    }
    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);
        // activates the SdbCtx now that it is initialised
        sdbCtx.activate();
        postFetch(query, res);
        return new SdbSienaIterable<T>(this, res.getItems(), query);
    } else {
        // we prepare the query each time
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = sdb.select(req);
        postFetch(query, res);
        return new SdbSienaIterable<T>(this, res.getItems(), query);
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) ArrayList(java.util.ArrayList) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Aggregations

SelectResult (com.amazonaws.services.simpledb.model.SelectResult)22 SelectRequest (com.amazonaws.services.simpledb.model.SelectRequest)12 Item (com.amazonaws.services.simpledb.model.Item)11 AmazonClientException (com.amazonaws.AmazonClientException)7 ReplaceableAttribute (com.amazonaws.services.simpledb.model.ReplaceableAttribute)5 ArrayList (java.util.ArrayList)5 Attribute (com.amazonaws.services.simpledb.model.Attribute)4 SienaException (siena.SienaException)4 AmazonSimpleDB (com.amazonaws.services.simpledb.AmazonSimpleDB)3 LinkedList (java.util.LinkedList)3 CreateDomainRequest (com.amazonaws.services.simpledb.model.CreateDomainRequest)2 DeleteDomainRequest (com.amazonaws.services.simpledb.model.DeleteDomainRequest)2 PutAttributesRequest (com.amazonaws.services.simpledb.model.PutAttributesRequest)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 NoSuchDomainException (com.amazonaws.services.simpledb.model.NoSuchDomainException)1 EventType (com.netflix.simianarmy.EventType)1 MonkeyType (com.netflix.simianarmy.MonkeyType)1