DynamoDBMapper CRUD 操作 - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DynamoDBMapper CRUD 操作

下列 Java 程式碼範例會宣告具有 IdTitleISBNAuthors 屬性的 CatalogItem 類別。它使用註釋將這些屬性映射至 DynamoDB 中的 ProductCatalog 資料表。範例接著會使用 DynamoDBMapper 來儲存、擷取和更新書籍物件,然後刪除書籍項目。

注意

此程式碼範例假設您已根據 在 DynamoDB 中建立資料表,以及載入程式碼範例的資料 一節的說明將資料載入 DynamoDB 的帳戶。

如需執行下列範例的 step-by-step 指示,請參閱Java 程式碼範例

匯入

import java.io.IOException; import java.util.Arrays; import java.util.HashSet; 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.DynamoDBTable;

Code

public class DynamoDBMapperCRUDExample { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); public static void main(String[] args) throws IOException { testCRUDOperations(); System.out.println("Example complete!"); } @DynamoDBTable(tableName = "ProductCatalog") public static class CatalogItem { private Integer id; private String title; private String ISBN; private Set<String> bookAuthors; // Partition key @DynamoDBHashKey(attributeName = "Id") public Integer getId() { return id; } public void setId(Integer 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 = "Authors") public Set<String> getBookAuthors() { return bookAuthors; } public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; } @Override public String toString() { return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors + ", id=" + id + ", title=" + title + "]"; } } 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."); } } }