Example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient query

List of usage examples for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient query

Introduction

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

Prototype

@Override
public QueryResult query(QueryRequest request) 

Source Link

Document

The Query operation finds items based on primary key values.

Usage

From source file:awslabs.lab22.SolutionCode.java

License:Open Source License

@Override
public QueryResult lookupByHashKey(AmazonDynamoDBClient ddbClient, String tableName, String company) {
    // Construct an AttributeValue object containing the provided company name.
    AttributeValue attributeValue = new AttributeValue().withS(company);
    // Construct a Condition object containing the desired comparison ("EQ") and the attribute value
    // containing the company name.
    Condition condition = new Condition().withComparisonOperator("EQ").withAttributeValueList(attributeValue);

    // Construct a QueryRequest object that performs a consistent read on the specified table for the
    // previously constructed condition.
    QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withConsistentRead(true);
    queryRequest.addKeyConditionsEntry("Company", condition);

    // Submit the query by calling the query method of the ddbClient object and return the result.
    return ddbClient.query(queryRequest);
}

From source file:awslabs.lab51.SolutionCode.java

License:Open Source License

@Override
public Boolean isImageInDynamo(AmazonDynamoDBClient dynamoDbClient, String tableName, String key) {
    QueryRequest queryRequest = new QueryRequest(tableName).withConsistentRead(true);
    queryRequest.addKeyConditionsEntry("Key",
            new Condition().withComparisonOperator("EQ").withAttributeValueList(new AttributeValue(key)));

    return (dynamoDbClient.query(queryRequest).getCount() > 0);
}

From source file:ch.qos.logback.more.appenders.DynamoDBLogbackAppender.java

License:Apache License

private static long getLastId(String tableName, String instanceName, AmazonDynamoDBClient dynamoClient) {
    QueryRequest queryRequest = new QueryRequest().withTableName(tableName)
            .withKeyConditionExpression("instance = :pk")
            .addExpressionAttributeValuesEntry(":pk", new AttributeValue().withS(instanceName))
            .withScanIndexForward(false).withLimit(1);
    QueryResult result = dynamoClient.query(queryRequest);
    List<Map<String, AttributeValue>> items = result.getItems();
    if (items == null || items.size() == 0) {
        return 0L;
    } else {/*from w w  w .j  av a 2 s  . co m*/
        return Long.valueOf(items.get(0).get("id").getN());
    }
}

From source file:com.trk.aboutme.DynamoDB.DynamoDBManagerBooks.java

License:Open Source License

public static ArrayList<Books> getBooksList(String tableName, String catagory) {

    AmazonDynamoDBClient ddb = Shelf.clientManager.ddb();
    try {/*ww w.j  a  va  2  s  .c o  m*/

        String bookId = catagory;
        long twoWeeksAgoMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L);
        Date twoWeeksAgo = new Date();
        twoWeeksAgo.setTime(twoWeeksAgoMilli);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String twoWeeksAgoStr = df.format(twoWeeksAgo);
        ArrayList<Books> resultList = new ArrayList<Books>();

        Map<String, AttributeValue> lastEvaluatedKey = null;
        do {

            Condition hashKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ.toString())
                    .withAttributeValueList(new AttributeValue().withS(bookId));

            Condition rangeKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.GT.toString())
                    .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr));

            Map<String, Condition> keyConditions = new HashMap<String, Condition>();
            keyConditions.put("bookID", hashKeyCondition);
            keyConditions.put("posteddate", rangeKeyCondition);

            QueryRequest queryRequest = new QueryRequest().withTableName(tableName)
                    .withKeyConditions(keyConditions).withSelect("title, datecreated").withLimit(5)
                    .withExclusiveStartKey(lastEvaluatedKey);

            QueryResult result = ddb.query(queryRequest);
            for (Map<String, AttributeValue> item : result.getItems()) {
                for (Map.Entry<String, AttributeValue> i : item.entrySet()) {

                    Books b = new Books();
                    AttributeValue value = i.getValue();
                    String vs = value.getS();

                    if (i.getKey().equals("title"))
                        b.setM_title(vs == null ? "" : vs);
                    if (i.getKey().equals("datecreated"))
                        b.setM_dateCreated(vs == null ? "" : vs);

                    resultList.add(b);
                }
            }
            lastEvaluatedKey = result.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);

        return resultList;

    } catch (AmazonServiceException ex) {
        Shelf.clientManager.wipeCredentialsOnAuthError(ex);
    }

    return null;
}

From source file:com.trk.aboutme.DynamoDB.DynamoDBManagerBooks.java

License:Open Source License

public static ArrayList<Pages> getPageList(String tableName, String catagory, String book) {

    AmazonDynamoDBClient ddb = Shelf.clientManager.ddb();
    try {//w ww.  jav  a  2 s.c  o m

        String pageId = catagory + "#" + book;
        long twoWeeksAgoMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L);
        Date twoWeeksAgo = new Date();
        twoWeeksAgo.setTime(twoWeeksAgoMilli);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String twoWeeksAgoStr = df.format(twoWeeksAgo);
        ArrayList<Pages> resultList = new ArrayList<Pages>();

        Map<String, AttributeValue> lastEvaluatedKey = null;
        do {

            Condition hashKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ.toString())
                    .withAttributeValueList(new AttributeValue().withS(pageId));

            Condition rangeKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.GT.toString())
                    .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr));

            Map<String, Condition> keyConditions = new HashMap<String, Condition>();
            keyConditions.put("pageID", hashKeyCondition);
            keyConditions.put("datecreated", rangeKeyCondition);

            QueryRequest queryRequest = new QueryRequest().withTableName(tableName)
                    .withKeyConditions(keyConditions).withSelect("title, message, authorphoto, posteddate")
                    .withLimit(5).withExclusiveStartKey(lastEvaluatedKey);

            QueryResult result = ddb.query(queryRequest);
            for (Map<String, AttributeValue> item : result.getItems()) {
                for (Map.Entry<String, AttributeValue> i : item.entrySet()) {
                    Pages p = new Pages();

                    AttributeValue value = i.getValue();
                    String vs = value.getS();
                    if (i.getKey().equals("title"))
                        p.setM_title(vs == null ? "" : vs);
                    if (i.getKey().equals("message"))
                        p.setM_message(vs == null ? "" : vs);
                    if (i.getKey().equals("authorphoto"))
                        p.setM_authorPhoto(vs == null ? "" : vs);
                    if (i.getKey().equals("posteddate"))
                        p.setM_postedDate(vs == null ? "" : vs);

                    resultList.add(p);
                }
            }
            lastEvaluatedKey = result.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);

        return resultList;

    } catch (AmazonServiceException ex) {
        Shelf.clientManager.wipeCredentialsOnAuthError(ex);
    }

    return null;
}