Example usage for com.amazonaws.services.simpledb.model SelectResult getItems

List of usage examples for com.amazonaws.services.simpledb.model SelectResult getItems

Introduction

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

Prototype


public java.util.List<Item> getItems() 

Source Link

Document

A list of items that match the select expression.

Usage

From source file:assign1.simpledb.java

License:Open Source License

public List<record> query(String word) throws Exception {
    /*/*from   ww w  . ja 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 = "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 {
    /*// www .  j a  v a  2s.  com
     * 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:br.com.ingenieux.mojo.simpledb.cmd.DumpDomainCommand.java

License:Apache License

public boolean execute(DumpDomainContext context) throws Exception {
    SelectRequest selectRequest = new SelectRequest(String.format("SELECT * FROM %s", context.getDomain()));
    SelectResult selectResult = service.select(selectRequest);

    ArrayNode rootNode = mapper.createArrayNode();

    while (!selectResult.getItems().isEmpty()) {
        for (Item item : selectResult.getItems())
            appendResult(rootNode, item);

        if (isBlank(selectResult.getNextToken()))
            break;

        selectResult = service.select(selectRequest.withNextToken(selectResult.getNextToken()));
    }/*from w  ww .  ja v  a 2s  .c  om*/

    FileWriter writer = new FileWriter(context.getOutputFile());

    writeData(rootNode, writer);

    IOUtil.close(writer);

    return false;
}

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

License:Apache License

/**
 * Get the instance details from SimpleDB
 *
 * @param app Cluster name//from ww  w.j a va 2s. co m
 * @param id  Node ID
 * @return the node with the given {@code id}, or {@code null} if no such node exists
 */
public PriamInstance getInstance(String app, String dc, int id) {
    AmazonSimpleDBClient simpleDBClient = getSimpleDBClient();
    SelectRequest request = new SelectRequest(String.format(INSTANCE_QUERY, app, dc, id));
    SelectResult result = simpleDBClient.select(request);
    if (result.getItems().size() == 0)
        return null;
    return transform(result.getItems().get(0));
}

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/*  w ww. j  a  va  2  s  .  c  o  m*/
 * @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

/**
 * //  w  w w.ja v  a2s.co 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.brighttag.agathon.dao.sdb.SdbCassandraInstanceDao.java

License:Apache License

@Override
public ImmutableSet<CassandraInstance> findAll(String ring) {
    List<CassandraInstance> instances = Lists.newArrayList();
    String nextToken = null;//from w w w  .j  a  v  a2 s .c  o  m

    do {
        SelectRequest request = new SelectRequest(String.format(ALL_QUERY, domain(ring)))
                .withNextToken(nextToken);
        SelectResult result = client.select(request);

        for (Item item : result.getItems()) {
            instances.add(transform(item));
        }

        nextToken = result.getNextToken();
    } while (nextToken != null);

    return ImmutableSet.copyOf(instances);
}

From source file:com.brighttag.agathon.dao.sdb.SdbCassandraInstanceDao.java

License:Apache License

@Override
public @Nullable CassandraInstance findById(String ring, int id) {
    SelectRequest request = new SelectRequest(String.format(INSTANCE_QUERY, domain(ring), id));
    SelectResult result = client.select(request);

    if (result.getItems().size() == 0) {
        return null;
    }//from   w  w  w. ja  v a 2s.c o m

    return transform(result.getItems().get(0));
}

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

License:Apache License

/**
 * {@link SimpleDBDomain}????????/* ww w .java2 s .  c o  m*/
 * 
 * @param clazz
 *            {@link SimpleDBDomain}????POJO
 * @param expression
 *            where
 * @param consistentRead
 *            ??
 * 
 *            <a href=
 *            "http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/ConsistencySummary.html"
 *            >AWS?</a>
 */
public <T> int count(Class<T> clazz, QueryExpression expression) {
    String whereExpression = null;
    if (expression != null) {
        whereExpression = expression.describe();
    }
    String query = createQuery(clazz, true, whereExpression, 0);
    SelectResult result = this.sdb.select(new SelectRequest(query, this.config.isConsistentRead()));
    String countValue = result.getItems().get(0).getAttributes().get(0).getValue();
    return Integer.parseInt(countValue);
}

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;
    }/*from w  w w  .  ja  v  a  2  s  . c  o  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;
}