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

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

Introduction

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

Prototype

@Override
    public ItemCollection<QueryOutcome> query(QuerySpec spec) 

Source Link

Usage

From source file:NYSEQuery.java

License:Open Source License

private static void query(String tableName) {
    Table table = null;
    try {/* ww w  .  j a  v a  2 s .com*/
        // Create table if it does not exist yet
        if (!Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is does not exist");
        } else {
            table = dynamo.getTable(tableName);
        }

        // http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html
        // select stockTicker, tradeDate, v from stock_eod 
        // where stockTicker = 'LEA' and tradeDate between '02-Oct-2013' and '03-Oct-2013'
        QuerySpec spec = new QuerySpec().withProjectionExpression("stockTicker,tradeDate,v")
                .withKeyConditionExpression("stockTicker = :v_st and tradeDate between :v_sd and :v_ed")
                //               .withFilterExpression("contains(tradeDate, :v_t)")
                .withValueMap(new ValueMap().withString(":v_st", "LEA").withString(":v_sd", "02-Oct-2013")
                        .withString(":v_ed", "03-Oct-2013")
                // .withString(":v_td", "01-Oct-2013")
                //                     .withString(":v_t", "Oct")
                ).withConsistentRead(true);

        ItemCollection<QueryOutcome> items = table.query(spec);

        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

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

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 {/*from  w  w  w  . j  a  va  2s  .com*/
            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.eho.dynamodb.DynamoDBConnection.java

public static ItemCollection<QueryOutcome> query_dynamodb(QuerySpec spec) {
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);
    ItemCollection<QueryOutcome> items = table.query(spec);
    return items;
}

From source file:com.innoq.hagmans.bachelor.DynamoDBUtils.java

License:Open Source License

/**
 * Persists the given temperatures on DynamoDB
 * //w  ww.  java2s.c om
 * @param tableName
 *            The name of the table, where the records will be persisted
 * @param temperatureMap
 *            A map containing the sensor names as the key, and as the value
 *            a hashmap with the timestamp of the temperature as the key and
 *            the temperature as the value
 * @param timestamp
 *            The timestamp of the run
 */
public void putTemperatures(String tableName, HashMap<String, HashMap<String, String>> temperatureMap,
        long timestamp) {

    Table table = dynamoDB.getTable(tableName);

    for (String sensor : temperatureMap.keySet()) {
        QuerySpec spec = new QuerySpec().withHashKey(ATTRIBUTE_NAME_HASH_KEY, sensor).withRangeKeyCondition(
                new RangeKeyCondition(ATTRIBUTE_NAME_RANGE_KEY).eq(String.valueOf(timestamp)));

        ItemCollection<QueryOutcome> items = table.query(spec);

        Iterator<Item> iterator = items.iterator();
        Item item = null;
        Map<String, String> temperatures = null;
        while (iterator.hasNext()) {
            item = iterator.next();
            temperatures = item.getMap(ATTRIBUTE_NAME_TEMPERATURE);
        }

        if (temperatures == null) {
            temperatures = new HashMap<>();
        }
        temperatures.putAll(temperatureMap.get(sensor));
        table.putItem(new Item().withPrimaryKey(ATTRIBUTE_NAME_HASH_KEY, sensor, ATTRIBUTE_NAME_RANGE_KEY,
                String.valueOf(timestamp)).withMap(ATTRIBUTE_NAME_TEMPERATURE, temperatures));
        System.out.println("PutItem succeeded!");
    }
}

From source file:com.innoq.hagmans.bachelor.DynamoDBUtils.java

License:Open Source License

/**
 * Gibt eine @HashMap mit allen Temperaturen zurck fr den bergebenen
 * Sensor//from  w  w  w. java2 s  . c om
 * 
 * @param sensor
 * @param tableName
 * @return @Hashmap, die als Key einen Timestamp enthalten, zu dessen
 *         Zeitpunkt die Daten des Sensors erfasst werden und die Values
 *         sind eine Liste der Temperaturen des Sensors zum Timestamp
 */
public HashMap<String, HashMap<String, Object>> getTemperaturesForSensor(String sensor, String tableName) {
    Table table = dynamoDB.getTable(tableName);

    QuerySpec spec = new QuerySpec().withHashKey(ATTRIBUTE_NAME_HASH_KEY, sensor);

    ItemCollection<QueryOutcome> items = table.query(spec);

    Iterator<Item> iterator = items.iterator();
    Item item = null;
    HashMap<String, HashMap<String, Object>> temperatureMap = new HashMap<>();
    while (iterator.hasNext()) {
        item = iterator.next();
        temperatureMap.put(item.getString(ATTRIBUTE_NAME_RANGE_KEY),
                new HashMap<>(item.getMap(ATTRIBUTE_NAME_TEMPERATURE)));
    }

    return temperatureMap;
}

From source file:mx.iteso.desi.cloud.keyvalue.DynamoDBStorage.java

License:Apache License

@Override
public Set<String> get(String search) {
    Set<String> ret = new HashSet<String>();
    Table table = docClient.getTable(dbName);

    QuerySpec spec = new QuerySpec().withKeyConditionExpression("keyword = :v_kw")
            .withValueMap(new ValueMap().withString(":v_kw", search));

    ItemCollection<QueryOutcome> items = table.query(spec);

    Iterator<Item> iterator = items.iterator();
    Item item = null;/*from   ww w .ja v  a  2 s.c o  m*/
    while (iterator.hasNext()) {
        item = iterator.next();
        ret.add(item.get("value").toString());
    }
    return ret;
}