Java Code Examples for com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#query()

The following examples show how to use com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#query() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashKeyOnlyTableWithGSIITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can query using the hash/range GSI on our hash-key only
 * table.
 */
@Test
public void testGSIQuery() throws Exception {
    DynamoDBMapper mapper = TestDynamoDBMapperFactory
            .createDynamoDBMapper(dynamo);
    String status = "foo-status";

    User user = new User();
    user.setId("123");
    user.setStatus(status);
    user.setTs("321");
    mapper.save(user);

    DynamoDBQueryExpression<User> expr = new DynamoDBQueryExpression<User>()
            .withIndexName("statusAndCreation")
            .withLimit(100)
            .withConsistentRead(false)
            .withHashKeyValues(user)
            .withRangeKeyCondition(
                    "ts",
                    new Condition()
                    .withComparisonOperator(ComparisonOperator.GT)
                    .withAttributeValueList(new AttributeValue("100")));

    PaginatedQueryList<User> query = mapper.query(User.class, expr);
    int size = query.size();
    if (DEBUG)
        System.err.println("size=" + size);
    assertTrue(1 == size);
    assertEquals(status, query.get(0).getStatus());
}
 
Example 2
Source File: DynamoDBMapperQueryScanExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void FindRepliesInLast15Days(DynamoDBMapper mapper, String forumName, String threadSubject)
    throws Exception {
    System.out.println("FindRepliesInLast15Days: Replies within last 15 days.");

    String partitionKey = forumName + "#" + threadSubject;

    long twoWeeksAgoMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
    Date twoWeeksAgo = new Date();
    twoWeeksAgo.setTime(twoWeeksAgoMilli);
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);

    Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
    eav.put(":val1", new AttributeValue().withS(partitionKey));
    eav.put(":val2", new AttributeValue().withS(twoWeeksAgoStr.toString()));

    DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
        .withKeyConditionExpression("Id = :val1 and ReplyDateTime > :val2").withExpressionAttributeValues(eav);

    List<Reply> latestReplies = mapper.query(Reply.class, queryExpression);

    for (Reply reply : latestReplies) {
        System.out.format("Id=%s, Message=%s, PostedBy=%s %n, ReplyDateTime=%s %n", reply.getId(),
            reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime());
    }
}
 
Example 3
Source File: DynamoDBMapperQueryScanExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void FindRepliesPostedWithinTimePeriod(DynamoDBMapper mapper, String forumName, String threadSubject)
    throws Exception {
    String partitionKey = forumName + "#" + threadSubject;

    System.out.println(
        "FindRepliesPostedWithinTimePeriod: Find replies for thread Message = 'DynamoDB Thread 2' posted within a period.");
    long startDateMilli = (new Date()).getTime() - (14L * 24L * 60L * 60L * 1000L); // Two
                                                                                    // weeks
                                                                                    // ago.
    long endDateMilli = (new Date()).getTime() - (7L * 24L * 60L * 60L * 1000L); // One
                                                                                 // week
                                                                                 // ago.
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String startDate = dateFormatter.format(startDateMilli);
    String endDate = dateFormatter.format(endDateMilli);

    Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
    eav.put(":val1", new AttributeValue().withS(partitionKey));
    eav.put(":val2", new AttributeValue().withS(startDate));
    eav.put(":val3", new AttributeValue().withS(endDate));

    DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
        .withKeyConditionExpression("Id = :val1 and ReplyDateTime between :val2 and :val3")
        .withExpressionAttributeValues(eav);

    List<Reply> betweenReplies = mapper.query(Reply.class, queryExpression);

    for (Reply reply : betweenReplies) {
        System.out.format("Id=%s, Message=%s, PostedBy=%s %n, PostedDateTime=%s %n", reply.getId(),
            reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime());
    }

}
 
Example 4
Source File: QueryITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that exception should be raised when user provides an index name
 * when making query with the primary range key.
 */
@Test
public void testUnnecessaryIndexNameException() {
    try {
        DynamoDBMapper mapper = TestDynamoDBMapperFactory
                .createDynamoDBMapper(dynamo);
        long hashKey = System.currentTimeMillis();
        RangeKeyTestClass keyObject = new RangeKeyTestClass();
        keyObject.setKey(hashKey);
        DynamoDBQueryExpression<RangeKeyTestClass> queryExpression = new DynamoDBQueryExpression<RangeKeyTestClass>()
                .withHashKeyValues(keyObject);
        queryExpression
                .withRangeKeyCondition(
                        "rangeKey",
                        new Condition().withComparisonOperator(
                                ComparisonOperator.GT.toString())
                                .withAttributeValueList(
                                        new AttributeValue().withN("1.0")))
                .withLimit(11).withIndexName("some_index");
        mapper.query(RangeKeyTestClass.class, queryExpression);
        fail("User should not provide index name when making query with the primary range key");
    } catch (IllegalArgumentException expected) {
        System.out.println(expected.getMessage());
    } catch (Exception e) {
        fail("Should trigger AmazonClientException.");
    }

}
 
Example 5
Source File: MapperLoadingStrategyConfigITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
private static PaginatedList<RangeKeyTestClass> getTestPaginatedQueryList(PaginationLoadingStrategy paginationLoadingStrategy) {
    DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig(ConsistentReads.CONSISTENT);
    DynamoDBMapper mapper = new DynamoDBMapper(dynamo, mapperConfig);
    
    // Construct the query expression for the tested hash-key value and any range-key value greater that 1.0
    RangeKeyTestClass keyObject = new RangeKeyTestClass();
    keyObject.setKey(hashKey);
    DynamoDBQueryExpression<RangeKeyTestClass> queryExpression = new DynamoDBQueryExpression<RangeKeyTestClass>().withHashKeyValues(keyObject);
    queryExpression.withRangeKeyCondition("rangeKey",
            new Condition().withComparisonOperator(ComparisonOperator.GT.toString()).withAttributeValueList(
                    new AttributeValue().withN("1.0"))).withLimit(PAGE_SIZE);
    
    return mapper.query(RangeKeyTestClass.class, queryExpression, new DynamoDBMapperConfig(paginationLoadingStrategy));
}
 
Example 6
Source File: ObjectPersistenceQueryScanExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void FindRepliesInLast15Days(DynamoDBMapper mapper, 
                                         String forumName, 
                                         String threadSubject) throws Exception {
 System.out.println("FindRepliesInLast15Days: Replies within last 15 days.");

 String hashKey = forumName + "#" + threadSubject;

 long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
 Date twoWeeksAgo = new Date();
 twoWeeksAgo.setTime(twoWeeksAgoMilli);
 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
 dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
 String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);

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

 Reply replyKey = new Reply();
 replyKey.setId(hashKey);
 
 DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
     .withHashKeyValues(replyKey)
     .withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);

 List<Reply> latestReplies = mapper.query(Reply.class, queryExpression);
 
 for (Reply reply : latestReplies) {
     System.out.format("Id=%s, Message=%s, PostedBy=%s %n, ReplyDateTime=%s %n", 
             reply.getId(), reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime() );
 }
}
 
Example 7
Source File: ObjectPersistenceQueryScanExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void FindRepliesPostedWithinTimePeriod(
        DynamoDBMapper mapper,
        String forumName, 
        String threadSubject) throws Exception {
    String hashKey = forumName + "#" + threadSubject;

    System.out.println("FindRepliesPostedWithinTimePeriod: Find replies for thread Message = 'DynamoDB Thread 2' posted within a period.");
    long startDateMilli = (new Date()).getTime() - (14L*24L*60L*60L*1000L); // Two weeks ago.
    long endDateMilli = (new Date()).getTime() - (7L*24L*60L*60L*1000L);    // One week ago.
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String startDate = dateFormatter.format(startDateMilli);
    String endDate = dateFormatter.format(endDateMilli);
    
    Condition rangeKeyCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.BETWEEN.toString())
        .withAttributeValueList(new AttributeValue().withS(startDate), 
                                new AttributeValue().withS(endDate));
    
    Reply replyKey = new Reply();
    replyKey.setId(hashKey);
    
    DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
        .withHashKeyValues(replyKey)
        .withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);
            
    List<Reply> betweenReplies = mapper.query(Reply.class, queryExpression);
    
    for (Reply reply : betweenReplies) {
        System.out.format("Id=%s, Message=%s, PostedBy=%s %n, PostedDateTime=%s %n", 
                reply.getId(), reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime() );
    }

}