Search in sources :

Example 16 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBMapperQueryScanExample method FindRepliesInLast15Days.

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());
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 17 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBMapperQueryScanExample method main.

public static void main(String[] args) throws Exception {
    try {
        DynamoDBMapper mapper = new DynamoDBMapper(client);
        // Get a book - Id=101
        GetBook(mapper, 101);
        // Sample forum and thread to test queries.
        String forumName = "Amazon DynamoDB";
        String threadSubject = "DynamoDB Thread 1";
        // Sample queries.
        FindRepliesInLast15Days(mapper, forumName, threadSubject);
        FindRepliesPostedWithinTimePeriod(mapper, forumName, threadSubject);
        // Scan a table and find book items priced less than specified
        // value.
        FindBooksPricedLessThanSpecifiedValue(mapper, "20");
        // Scan a table with multiple threads and find bicycle items with a
        // specified bicycle type
        int numberOfThreads = 16;
        FindBicyclesOfSpecificTypeWithMultipleThreads(mapper, numberOfThreads, "Road");
        System.out.println("Example complete!");
    } catch (Throwable t) {
        System.err.println("Error running the DynamoDBMapperQueryScanExample: " + t);
        t.printStackTrace();
    }
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)

Example 18 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBMapperBatchWriteExample method testBatchWrite.

private static void testBatchWrite(DynamoDBMapper mapper) {
    // Create Forum item to save
    Forum forumItem = new Forum();
    forumItem.name = "Test BatchWrite Forum";
    forumItem.threads = 0;
    forumItem.category = "Amazon Web Services";
    // Create Thread item to save
    Thread threadItem = new Thread();
    threadItem.forumName = "AmazonDynamoDB";
    threadItem.subject = "My sample question";
    threadItem.message = "BatchWrite message";
    List<String> tags = new ArrayList<String>();
    tags.add("batch operations");
    tags.add("write");
    threadItem.tags = new HashSet<String>(tags);
    // Load ProductCatalog item to delete
    Book book3 = mapper.load(Book.class, 903);
    List<Object> objectsToWrite = Arrays.asList(forumItem, threadItem);
    List<Book> objectsToDelete = Arrays.asList(book3);
    DynamoDBMapperConfig config = DynamoDBMapperConfig.builder().withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.CLOBBER).build();
    mapper.batchWrite(objectsToWrite, objectsToDelete, config);
}
Also used : DynamoDBMapperConfig(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig) ArrayList(java.util.ArrayList)

Example 19 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBMapperExample method main.

public static void main(String[] args) throws IOException {
    // Set the AWS region you want to access.
    Regions usWest2 = Regions.US_WEST_2;
    client = AmazonDynamoDBClientBuilder.standard().withRegion(usWest2).build();
    DimensionType dimType = new DimensionType();
    dimType.setHeight("8.00");
    dimType.setLength("11.0");
    dimType.setThickness("1.0");
    Book book = new Book();
    book.setId(502);
    book.setTitle("Book 502");
    book.setISBN("555-5555555555");
    book.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author2")));
    book.setDimensions(dimType);
    DynamoDBMapper mapper = new DynamoDBMapper(client);
    mapper.save(book);
    Book bookRetrieved = mapper.load(Book.class, 502);
    System.out.println("Book info: " + "\n" + bookRetrieved);
    bookRetrieved.getDimensions().setHeight("9.0");
    bookRetrieved.getDimensions().setLength("12.0");
    bookRetrieved.getDimensions().setThickness("2.0");
    mapper.save(bookRetrieved);
    bookRetrieved = mapper.load(Book.class, 502);
    System.out.println("Updated book info: " + "\n" + bookRetrieved);
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Regions(com.amazonaws.regions.Regions)

Example 20 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBMapperCRUDExample method testCRUDOperations.

private static void testCRUDOperations() {
    CatalogItem item = new CatalogItem();
    item.setId(601);
    item.setTitle("Book 601");
    item.setISBN("611-1111111111");
    item.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author2")));
    // Save the item (book).
    DynamoDBMapper mapper = new DynamoDBMapper(client);
    mapper.save(item);
    // Retrieve the item.
    CatalogItem itemRetrieved = mapper.load(CatalogItem.class, 601);
    System.out.println("Item retrieved:");
    System.out.println(itemRetrieved);
    // Update the item.
    itemRetrieved.setISBN("622-2222222222");
    itemRetrieved.setBookAuthors(new HashSet<String>(Arrays.asList("Author1", "Author3")));
    mapper.save(itemRetrieved);
    System.out.println("Item updated:");
    System.out.println(itemRetrieved);
    // Retrieve the updated item.
    DynamoDBMapperConfig config = DynamoDBMapperConfig.builder().withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT).build();
    CatalogItem updatedItem = mapper.load(CatalogItem.class, 601, config);
    System.out.println("Retrieved the previously updated item:");
    System.out.println(updatedItem);
    // Delete the item.
    mapper.delete(updatedItem);
    // Try to retrieve deleted item.
    CatalogItem deletedItem = mapper.load(CatalogItem.class, updatedItem.getId(), config);
    if (deletedItem == null) {
        System.out.println("Done - Sample item is deleted.");
    }
}
Also used : DynamoDBMapperConfig(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)

Aggregations

DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)17 IOException (java.io.IOException)5 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)4 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 GoraException (org.apache.gora.util.GoraException)4 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)3 Date (java.util.Date)3 Before (org.junit.Before)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDBMapperConfig (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)2 DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)2 DynamoDBScanExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)2 ProductInfo (com.baeldung.dynamodb.entity.ProductInfo)2 ProductInfoRepository (com.baeldung.dynamodb.repository.ProductInfoRepository)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2