Example usage for com.amazonaws.services.simpledb.model SelectRequest setNextToken

List of usage examples for com.amazonaws.services.simpledb.model SelectRequest setNextToken

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.model SelectRequest setNextToken.

Prototype


public void setNextToken(String nextToken) 

Source Link

Document

A string informing Amazon SimpleDB where to start the next list of ItemNames.

Usage

From source file:assign1.simpledb.java

License:Open Source License

public List<record> query(String word) throws Exception {
    /*/*from  ww  w  .j  a  v  a  2  s  .  c o m*/
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials*/

    ArrayList<record> result = new ArrayList<record>();
    AWSCredentials credentials = null;
    try {
        credentials = new PropertiesCredentials(
                simpledb.class.getResourceAsStream("AwsCredentials.properties"));
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/huanglin/.aws/credentials), and is in valid format.", e);
    }

    AmazonSimpleDB sdb = new AmazonSimpleDBClient(credentials);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SimpleDB");
    System.out.println("===========================================\n");

    String myDomain = "MyStore";

    String filter = word;
    String nextToken = null;
    SelectResult selectResult = null;
    do {
        String selectExpression = "";
        if (word.equals("all"))
            selectExpression = "select * from MyStore LIMIT 2500";
        else
            selectExpression = "select * from " + myDomain + " where content like '%" + filter
                    + "%' LIMIT 2500";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        selectRequest.setNextToken(nextToken);
        selectResult = sdb.select(selectRequest);
        nextToken = selectResult.getNextToken();
        List<Item> list = selectResult.getItems();
        for (Item item : list) {
            System.out.println("    itemIndex: " + item.getName());
            List<Attribute> attributeTmp = new ArrayList<Attribute>();
            attributeTmp = item.getAttributes();
            record tmp = new record();
            for (int i = 0; i < 5; i++) {
                if (attributeTmp.get(i).getName().equals("content"))
                    tmp.content = attributeTmp.get(i).getValue();
                else if (attributeTmp.get(i).getName().equals("geoLat"))
                    tmp.x = Double.parseDouble(attributeTmp.get(i).getValue());
                else if (attributeTmp.get(i).getName().equals("Username"))
                    tmp.username = attributeTmp.get(i).getValue();
                else if (attributeTmp.get(i).getName().equals("Location"))
                    tmp.location = attributeTmp.get(i).getValue();
                else
                    tmp.y = Double.parseDouble(attributeTmp.get(i).getValue());
            }
            result.add(tmp);
        }
    } while (nextToken != null);

    return result;
}

From source file:assign1.simpledb1.java

License:Open Source License

public List<record> query(String word) throws Exception {
    /*/*from w w w .j  a  va 2  s  .c  o m*/
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials*/

    ArrayList<record> result = new ArrayList<record>();
    AWSCredentials credentials = null;
    try {
        credentials = new PropertiesCredentials(
                simpledb.class.getResourceAsStream("AwsCredentials.properties"));
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/huanglin/.aws/credentials), and is in valid format.", e);
    }

    AmazonSimpleDB sdb = new AmazonSimpleDBClient(credentials);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SimpleDB");
    System.out.println("===========================================\n");

    String myDomain = "Twitter";

    String filter = word;
    String nextToken = null;
    SelectResult selectResult = null;
    do {
        String selectExpression = "";
        if (word.equals("all"))
            selectExpression = "select * from MyStore LIMIT 2500";
        else
            selectExpression = "select * from " + myDomain + " where content like '%" + filter
                    + "%' LIMIT 2500";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        selectRequest.setNextToken(nextToken);
        selectResult = sdb.select(selectRequest);
        nextToken = selectResult.getNextToken();
        List<Item> list = selectResult.getItems();
        for (Item item : list) {
            System.out.println("    itemIndex: " + item.getName());
            List<Attribute> attributeTmp = new ArrayList<Attribute>();
            attributeTmp = item.getAttributes();
            record tmp = new record();
            for (int i = 0; i < 5; i++) {
                if (attributeTmp.get(i).getName().equals("content"))
                    tmp.content = attributeTmp.get(i).getValue();
                else if (attributeTmp.get(i).getName().equals("geoLat"))
                    tmp.x = Double.parseDouble(attributeTmp.get(i).getValue());
                else if (attributeTmp.get(i).getName().equals("Username"))
                    tmp.username = attributeTmp.get(i).getValue();
                else if (attributeTmp.get(i).getName().equals("Location"))
                    tmp.location = attributeTmp.get(i).getValue();
                else
                    tmp.y = Double.parseDouble(attributeTmp.get(i).getValue());
            }
            result.add(tmp);
        }
    } while (nextToken != null);

    return result;
}

From source file:c3.ops.priam.aws.SDBInstanceData.java

License:Apache License

/**
 * Get the set of all nodes in the cluster
 *
 * @param app Cluster name/*from   w ww.ja  va 2  s .  com*/
 * @return the set of all instances in the given {@code app}
 */
public Set<PriamInstance> getAllIds(String app) {
    AmazonSimpleDBClient simpleDBClient = getSimpleDBClient();
    Set<PriamInstance> inslist = new HashSet<PriamInstance>();
    String nextToken = null;
    do {
        SelectRequest request = new SelectRequest(String.format(ALL_QUERY, app));
        request.setNextToken(nextToken);
        SelectResult result = simpleDBClient.select(request);
        nextToken = result.getNextToken();
        Iterator<Item> itemiter = result.getItems().iterator();
        while (itemiter.hasNext()) {
            inslist.add(transform(itemiter.next()));
        }

    } while (nextToken != null);
    return inslist;
}

From source file:com.aipo.aws.simpledb.SimpleDB.java

License:Open Source License

/**
 * /*from   w  ww  .j a  v  a  2 s  . c o  m*/
 * @param <M>
 * @param client
 * @param rootClass
 * @param sql
 * @param nextToken
 * @return
 */
public static <M> ResultList<M> select(AmazonSimpleDB client, Class<M> rootClass, String sql,
        String nextToken) {
    try {

        SelectRequest request = new SelectRequest(sql.toString()).withConsistentRead(true);
        if (nextToken != null) {
            request.setNextToken(nextToken);
        }
        SelectResult select = client.select(request);
        List<Item> items = select.getItems();
        ResultList<M> result = new ResultList<M>();
        for (Item item : items) {
            M model = rootClass.newInstance();
            if (model instanceof ResultItem) {
                ResultItem resultItem = (ResultItem) model;
                resultItem.assign(item);
                result.add(model);
            }
        }
        result.setNextToken(select.getNextToken());
        return result;
    } catch (InstantiationException e) {
        //
    } catch (IllegalAccessException e) {
        //
    }
    return new ResultList<M>();
}

From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java

License:Apache License

private <T> List<T> fetch(Class<T> clazz, String query) {
    SelectRequest selectRequest = new SelectRequest(query.toString(), this.config.isConsistentRead());
    if (this.selectNextToken != null) {
        selectRequest.setNextToken(this.selectNextToken);
        this.selectNextToken = null;
    }//  w w w  .j  av  a2 s  .co  m

    SelectResult result = this.sdb.select(selectRequest);
    List<Item> items = result.getItems();
    if (items.isEmpty()) {
        return Collections.emptyList();
    }
    this.selectNextToken = result.getNextToken();

    List<T> objects = new ArrayList<T>();
    Field itemNameField = this.reflector.findItemNameField(clazz);
    try {
        // SDB?item?
        for (Item item : items) {
            T instance;
            instance = clazz.newInstance();

            // ItemName?
            Class<?> type = itemNameField.getType();
            String itemName = item.getName();
            itemNameField.set(instance, this.reflector.decodeItemNameFromSimpleDBFormat(type, itemName));

            // item?attributes?
            List<Attribute> attrs = item.getAttributes();
            for (Attribute attr : attrs) {
                String attributeName = attr.getName();
                Field attrField = this.reflector.findFieldByAttributeName(clazz, attributeName);
                if (attrField == null) {
                    continue;
                }
                // Blob???LazyFetch?
                SimpleDBBlob blobAnno = attrField.getAnnotation(SimpleDBBlob.class);
                if (blobAnno != null) {
                    String fieldName = attrField.getName();
                    if (this.blobEagerFetchList.contains(fieldName)) {
                        // 
                        this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                    } else {
                        FetchType fetchType = blobAnno.fetch();
                        if (fetchType == FetchType.EAGER) {
                            // 
                            this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                        }
                    }
                } else {
                    this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                }

            }

            //
            objects.add(instance);
        }

    } catch (Exception e) {
        throw new SimpleDBMapperException(e);
    }

    return objects;
}

From source file:com.davidgildeh.hadoop.input.simpledb.SimpleDBDAO.java

License:Apache License

/**
 * Does a paged query in SimpleDB/* w ww. j a  v a2  s  .co  m*/
 *
 * @param query         The Select Query
 * @param nextToken     If there is a paging token to start from, or null if none
 * @return              The SelectResult from the query
 */
private SelectResult doQuery(String query, String nextToken) {

    SelectResult results = null;

    try {

        if (LOG.isDebugEnabled()) {
            LOG.debug("Running Query: " + query);
        }

        SelectRequest selectRequest = new SelectRequest(query);

        if (nextToken != null) {
            selectRequest.setNextToken(nextToken);
        }

        results = sdb.select(selectRequest);

    } catch (AmazonServiceException ase) {
        LOG.error("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SimpleDB, but was rejected with an error response for some reason.");
        LOG.error("Select Query:     " + query);
        LOG.error("Error Message:    " + ase.getMessage());
        LOG.error("HTTP Status Code: " + ase.getStatusCode());
        LOG.error("AWS Error Code:   " + ase.getErrorCode());
        LOG.error("Error Type:       " + ase.getErrorType());
        LOG.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        LOG.error("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with SimpleDB, "
                + "such as not being able to access the network.");
        LOG.error("Error Message: " + ace.getMessage());
    } finally {
        return results;
    }
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Get the stats for all levels a mathlete has played.
 *//*from  ww w. j a va 2s .  c  om*/
public Set<GameStatistics> fetchAllPlays(Mathlete mathlete) throws RequestException {
    String format = "select * from `%s` where `%s` = \"%s\" limit 2500";
    String query = String.format(format, sdbEscape(PLAYS_DOMAIN, '`'), sdbEscape(PLAYS_MATHLETE, '`'),
            sdbEscape(mathlete.id, '"'));

    Set<GameStatistics> plays = new HashSet<GameStatistics>();

    SelectRequest request = new SelectRequest(query, true);
    String nextToken = null;
    do {
        request.setNextToken(nextToken);
        SelectResult result = client.select(request);
        for (Item item : result.getItems()) {
            plays.add(parseStats(item));
        }
        nextToken = result.getNextToken();
    } while (nextToken != null);

    return plays;
}

From source file:com.netflix.dynomitemanager.sidecore.SimpleDBConfigSource.java

License:Apache License

@Override
public void initialize(final String asgName, final String region) {
    super.initialize(asgName, region);

    // End point is us-east-1
    AmazonSimpleDBClient simpleDBClient = new AmazonSimpleDBClient(provider.getAwsCredentialProvider());

    String nextToken = null;//from ww w . java 2 s . c om
    String appid = asgName.lastIndexOf('-') > 0 ? asgName.substring(0, asgName.indexOf('-')) : asgName;
    logger.info(String.format("appid used to fetch properties is: %s", appid));
    do {
        SelectRequest request = new SelectRequest(String.format(ALL_QUERY, appid));
        request.setNextToken(nextToken);
        SelectResult result = simpleDBClient.select(request);
        nextToken = result.getNextToken();
        Iterator<Item> itemiter = result.getItems().iterator();
        while (itemiter.hasNext())
            addProperty(itemiter.next());

    } while (nextToken != null);
}

From source file:com.netflix.simianarmy.aws.conformity.SimpleDBConformityClusterTracker.java

License:Apache License

private List<Item> querySimpleDBItems(String query) {
    Validate.notNull(query);/*from www .j  a v  a2  s.  com*/
    String nextToken = null;
    List<Item> items = new ArrayList<Item>();
    do {
        SelectRequest request = new SelectRequest(query);
        request.setNextToken(nextToken);
        request.setConsistentRead(Boolean.TRUE);
        SelectResult result = this.simpleDBClient.select(request);
        items.addAll(result.getItems());
        nextToken = result.getNextToken();
    } while (nextToken != null);

    return items;
}

From source file:com.zotoh.cloudapi.aws.SDB.java

License:Open Source License

@SuppressWarnings("serial")
@Override/*w ww  .j a v a2s .  c  om*/
public Map<String, Set<KeyValuePair>> query(String query, boolean consistentRead)
        throws CloudException, InternalException {
    tstEStrArg("query-string", query);
    Map<String, Set<KeyValuePair>> rc = MP();
    List<Item> lst;
    SelectRequest req;
    SelectResult res;
    String token = null;
    do {
        req = new SelectRequest().withSelectExpression(query).withConsistentRead(consistentRead);
        if (!isEmpty(token)) {
            req.setNextToken(token);
        }
        res = _svc.getCloud().getSDB().select(req);
        lst = res == null ? null : res.getItems();
        if (lst != null)
            for (int i = 0; i < lst.size(); ++i) {
                final Item itm = lst.get(i);
                rc.put(itm.getName(), new HashSet<KeyValuePair>() {
                    {
                        addAll(toKPs(itm.getAttributes()));
                    }
                });
            }
        token = res.getNextToken();
    } while (!isEmpty(token));
    return Collections.unmodifiableMap(rc);
}