DynamoDBMapper 批处理操作 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

DynamoDBMapper 批处理操作

以下 Java 代码示例声明了 BookForumThreadReply 类,并使用 DynamoDBMapper 类将它们映射到 Amazon DynamoDB 表。

该代码说明了以下批处理写入操作:

  • batchSave 将书本项目放置在 ProductCatalog 表。

  • batchDeleteProductCatalog 表删除项目。

  • batchWriteForumThread 表放入和删除项目。

有关此示例中使用的表的更多信息,请参阅为 DynamoDB 中的代码示例创建表和加载数据。有关测试以下示例的分步说明,请参阅 Java 代码示例

导入

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

代码

public class DynamoDBMapperBatchWriteExample { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); public static void main(String[] args) throws Exception { try { DynamoDBMapper mapper = new DynamoDBMapper(client); testBatchSave(mapper); testBatchDelete(mapper); testBatchWrite(mapper); System.out.println("Example complete!"); } catch (Throwable t) { System.err.println("Error running the DynamoDBMapperBatchWriteExample: " + t); t.printStackTrace(); } } private static void testBatchSave(DynamoDBMapper mapper) { Book book1 = new Book(); book1.setId(901); book1.setInPublication(true); book1.setISBN("902-11-11-1111"); book1.setPageCount(100); book1.setPrice(10); book1.setProductCategory("Book"); book1.setTitle("My book created in batch write"); Book book2 = new Book(); book2.setId(902); book2.setInPublication(true); book2.setISBN("902-11-12-1111"); book2.setPageCount(200); book2.setPrice(20); book2.setProductCategory("Book"); book2.setTitle("My second book created in batch write"); Book book3 = new Book(); book3.setId(903); book3.setInPublication(false); book3.setISBN("902-11-13-1111"); book3.setPageCount(300); book3.setPrice(25); book3.setProductCategory("Book"); book3.setTitle("My third book created in batch write"); System.out.println("Adding three books to ProductCatalog table."); mapper.batchSave(Arrays.asList(book1, book2, book3)); } private static void testBatchDelete(DynamoDBMapper mapper) { Book book1 = mapper.load(Book.class, 901); Book book2 = mapper.load(Book.class, 902); System.out.println("Deleting two books from the ProductCatalog table."); mapper.batchDelete(Arrays.asList(book1, book2)); } private static void testBatchWrite(DynamoDBMapper mapper) { // Create Forum item to save Forum forumItem = new Forum(); forumItem.setName("Test BatchWrite Forum"); forumItem.setThreads(0); forumItem.setCategory("Amazon Web Services"); // Create Thread item to save Thread threadItem = new Thread(); threadItem.setForumName("AmazonDynamoDB"); threadItem.setSubject("My sample question"); threadItem.setMessage("BatchWrite message"); List<String> tags = new ArrayList<String>(); tags.add("batch operations"); tags.add("write"); threadItem.setTags(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); } @DynamoDBTable(tableName = "ProductCatalog") public static class Book { private int id; private String title; private String ISBN; private int price; private int pageCount; private String productCategory; private boolean inPublication; // Partition key @DynamoDBHashKey(attributeName = "Id") public int getId() { return id; } public void setId(int id) { this.id = id; } @DynamoDBAttribute(attributeName = "Title") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @DynamoDBAttribute(attributeName = "ISBN") public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } @DynamoDBAttribute(attributeName = "Price") public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @DynamoDBAttribute(attributeName = "PageCount") public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } @DynamoDBAttribute(attributeName = "ProductCategory") public String getProductCategory() { return productCategory; } public void setProductCategory(String productCategory) { this.productCategory = productCategory; } @DynamoDBAttribute(attributeName = "InPublication") public boolean getInPublication() { return inPublication; } public void setInPublication(boolean inPublication) { this.inPublication = inPublication; } @Override public String toString() { return "Book [ISBN=" + ISBN + ", price=" + price + ", product category=" + productCategory + ", id=" + id + ", title=" + title + "]"; } } @DynamoDBTable(tableName = "Reply") public static class Reply { private String id; private String replyDateTime; private String message; private String postedBy; // Partition key @DynamoDBHashKey(attributeName = "Id") public String getId() { return id; } public void setId(String id) { this.id = id; } // Sort key @DynamoDBRangeKey(attributeName = "ReplyDateTime") public String getReplyDateTime() { return replyDateTime; } public void setReplyDateTime(String replyDateTime) { this.replyDateTime = replyDateTime; } @DynamoDBAttribute(attributeName = "Message") public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @DynamoDBAttribute(attributeName = "PostedBy") public String getPostedBy() { return postedBy; } public void setPostedBy(String postedBy) { this.postedBy = postedBy; } } @DynamoDBTable(tableName = "Thread") public static class Thread { private String forumName; private String subject; private String message; private String lastPostedDateTime; private String lastPostedBy; private Set<String> tags; private int answered; private int views; private int replies; // Partition key @DynamoDBHashKey(attributeName = "ForumName") public String getForumName() { return forumName; } public void setForumName(String forumName) { this.forumName = forumName; } // Sort key @DynamoDBRangeKey(attributeName = "Subject") public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } @DynamoDBAttribute(attributeName = "Message") public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @DynamoDBAttribute(attributeName = "LastPostedDateTime") public String getLastPostedDateTime() { return lastPostedDateTime; } public void setLastPostedDateTime(String lastPostedDateTime) { this.lastPostedDateTime = lastPostedDateTime; } @DynamoDBAttribute(attributeName = "LastPostedBy") public String getLastPostedBy() { return lastPostedBy; } public void setLastPostedBy(String lastPostedBy) { this.lastPostedBy = lastPostedBy; } @DynamoDBAttribute(attributeName = "Tags") public Set<String> getTags() { return tags; } public void setTags(Set<String> tags) { this.tags = tags; } @DynamoDBAttribute(attributeName = "Answered") public int getAnswered() { return answered; } public void setAnswered(int answered) { this.answered = answered; } @DynamoDBAttribute(attributeName = "Views") public int getViews() { return views; } public void setViews(int views) { this.views = views; } @DynamoDBAttribute(attributeName = "Replies") public int getReplies() { return replies; } public void setReplies(int replies) { this.replies = replies; } } @DynamoDBTable(tableName = "Forum") public static class Forum { private String name; private String category; private int threads; // Partition key @DynamoDBHashKey(attributeName = "Name") public String getName() { return name; } public void setName(String name) { this.name = name; } @DynamoDBAttribute(attributeName = "Category") public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } @DynamoDBAttribute(attributeName = "Threads") public int getThreads() { return threads; } public void setThreads(int threads) { this.threads = threads; } } }