Search in sources :

Example 1 with DynamoDBMapperConfig

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig 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 2 with DynamoDBMapperConfig

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig 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

DynamoDBMapperConfig (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)2 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)1 ArrayList (java.util.ArrayList)1