绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
亚马逊DynamoDB
2022-04-08 14:59:09

介绍

  • Amazon DynamoDB是一项完全托管的NoSQL数据库服务,可提供无缝的可扩展性和快速可预测的性能。
  • Amazon DynamoDB自动将表的数据和流量分布在足够数量的服务器上,以处理客户指定的请求容量和存储的数据量,同时保持一致且快速的性能。
  • 所有数据项都存储在固态磁盘(SSD)上,并自动跨区域中的多个可用区复制,以提供内置的高可用性和数据持久性。
  • 您可以通过AWS管理控制台启动新的Amazon DynamoDB数据库表,在不停机或性能下降的情况下扩展或缩减对该表的请求容量,并获得对资源利用率和性能指标的可见性。
  • 借助Amazon DynamoDB,您可以将操作和扩展分布式数据库的管理负担转移到AWS,因此您不必担心硬件设置,设置和配置,复制,软件修补或集群扩展。
  • SDK: http : //aws.amazon.com/sdkforjava/

Amazon DynamoDB注释

  • @DynamoDBTable

标识Amazon DynamoDB中的目标表。 例如,以下Java代码段定义了一个类Developer,并将其映射到Amazon DynamoDB中的People表。

  1. @DynamoDBTable(tableName="People")
  2. public class Developer { ...}
  • @DynamoDBIgnore

向DynamoDBMapper实例指示应该忽略关联的属性。 将数据保存到表时,DynamoDBMapper不会将此属性保存到表中。

  • @DynamoDBAttribute

将属性映射到表属性。 默认情况下,每个类属性都映射到具有相同名称的item属性。 但是,如果名称不同,则可以使用此标记将属性映射到属性。 在以下Java代码段中,DynamoDBAttribute将BookAuthors属性映射到表中的Authors属性名称。

  1. @DynamoDBAttribute(attributeName = "Authors")
  2. public List<String> getBookAuthors() { return BookAuthors; }
  3. public void setBookAuthors(List<String> BookAuthors) { this.BookAuthors = BookAuthors; }

将对象保存到表时,DynamoDBMapper使用作者作为属性名称。

  • @DynamoDBHashKey

将类属性映射到表的hash属性。 该属性必须是受支持的String或Numeric类型之一,并且不能是集合类型。

假设您有一个表ProductCatalog,该表具有ID作为主键。 以下Java代码段定义了CatalogItem类,并使用@DynamoDBHashKey标记将其id属性映射到ProductCatalog表的主键。

  1. @DynamoDBTable(tableName="ProductCatalog")
  2. public class CatalogItem {
  3. private String Id;
  4. @DynamoDBHashKey(attributeName="Id")
  5. public String getId() {
  6. return Id;
  7. }
  8. public void setId(String Id) {
  9. this.Id = Id;
  10. }
  11. // Additional properties go here.
  12. }
  • @DynamoDBRangeKey

将类属性映射到表的范围键属性。 如果主键是由哈希键和范围键属性组成的,则可以使用此标记将您的类字段映射到范围属性。 例如,假定您有一个“回复”表,其中存储有论坛主题的回复。 每个线程可以有很多回复。 因此,该表的主键是ThreadId和ReplyDateTime。 ThreadId是哈希属性,ReplyDateTime是范围属性。 以下Java代码段定义了Reply类,并将其映射到Reply表。 它同时使用@DynamoDBHashKey和@DynamoDBRangeKeytags来标识映射到主键的类属性。

  1. @DynamoDBTable(tableName="Reply")
  2. public class Reply {
  3. private String id;
  4. private String replyDateTime;
  5. @DynamoDBHashKey(attributeName="Id")
  6. public String getId() { return id; }
  7. public void setId(String id) { this.id = id; }
  8. @DynamoDBRangeKey(attributeName="ReplyDateTime")
  9. public String getReplyDateTime() { return replyDateTime; }
  10. public void setReplyDateTime(String replyDateTime) { this.replyDateTime = replyDateTime; }
  11. // Additional properties go here.
  12. }
  • @DynamoDBAutoGeneratedKey

将哈希键或范围键属性标记为自动生成。 保存这些属性时,对象持久性模型将生成一个随机UUID。 只能将字符串属性标记为自动生成的键。

以下代码段演示了如何使用自动生成的密钥。

  1. @DynamoDBTable(tableName="AutoGeneratedKeysExample")
  2. public class AutoGeneratedKeys {
  3. private String id;
  4. private String payload;
  5. @DynamoDBHashKey(attributeName = "Id")
  6. @DynamoDBAutoGeneratedKey
  7. public String getId() { return id; }
  8. public void setId(String id) { this.id = id; }
  9. @DynamoDBAttribute(attributeName="payload")
  10. public String getPayload() { return this.payload };
  11. public String setPayload(String payload) { this.payload = payload };
  12. public static void saveItem() {
  13. AutoGeneratedKeys obj = new AutoGeneratedKeys();
  14. obj.setPayload("abc123");
  15. // id field is null at this point
  16. DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
  17. mapper.save(obj);
  18. System.out.println("Object was saved with id " + obj.getId());
  19. }
  20. }
  • @DynamoDBVersionAttribute

标识用于存储乐观锁定版本号的类属性。 DynamoDBMapper保存新项目时,会为此属性分配一个版本号,并在每次更新该项目时将其递增。 仅支持数字标量类型。

DynamoDBMapper类别

DynamoDBMapper类是Amazon DynamoDB的入口点。 它提供了与Amazon DynamoDB的连接,并使您能够访问各种表中的数据,对项目执行各种CRUD操作以及对表执行查询和扫描。 此类提供以下关键操作,供您使用Amazon DynamoDB。

    • 将指定的对象保存到表中。
mapper.save(obj, new DynamoDBMapperConfig(DynamoDBMapperConfig.SaveBehavior.CLOBBER));
  • 加载
    • 从表中检索项目。
  1. CatalogItem item = mapper.load(CatalogItem.class, item.getId(),
  2. new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT));
  • 删除
    • 从表中删除一个项目。
  • 询问
    • 启用表查询。
  1. String forumName = "Amazon DynamoDB";
  2. String forumSubject = "DynamoDB Thread 1";
  3. String hashKey = forumName + "#" + forumSubject;
  4. long twoWeeksAgoMilli = (new Date()).getTime() - (14L*24L*60L*60L*1000L);
  5. Date twoWeeksAgo = new Date();
  6. twoWeeksAgo.setTime(twoWeeksAgoMilli);
  7. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  8. String twoWeeksAgoStr = df.format(twoWeeksAgo);
  9. Condition rangeKeyCondition = new Condition()
  10. .withComparisonOperator(ComparisonOperator.GT.toString())
  11. .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr.toString()));
  12. Reply replyKey = new Reply();
  13. replyKey.setId(hashKey);
  14. DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
  15. .withHashKeyValues(replyKey)
  16. .withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);
  17. List<Reply> latestReplies = mapper.query(Reply.class, queryExpression);

该查询返回Reply对象的集合。

  • 扫描
    • 扫描整个表。
  1. DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
  2. Map<String, Condition> scanFilter = new HashMap<String, Condition>();
  3. Condition scanCondition = new Condition()
  4. .withComparisonOperator(ComparisonOperator.EQ.toString())
  5. .withAttributeValueList(new AttributeValue().withN("0"));
  6. scanFilter.put("Answered", scanCondition);
  7. scanExpression.setScanFilter(scanFilter);
  8. List<Thread> unansweredThreads = mapper.scan(Thread.class, scanExpression);
  • 扫描方法返回“延迟加载”集合。 它初仅返回一页结果,然后在需要时对下一页进行服务调用。 要获取所有匹配项,您仅需要遍历unansweredThreads集合。
  • batchDelete
    • 使用对AmazonDynamoDB.batchWriteItem方法的一个或多个调用从一个或多个表中删除对象。
  1. Book book1 = mapper.load(Book.class, 901);
  2. Book book2 = mapper.load(Book.class, 902);
  3. mapper.batchDelete(Arrays.asList(book1, book2));
  • batchSave
    • 使用对AmazonDynamoDB.batchWriteItem方法的一个或多个调用将对象保存到一个或多个表。
  1. Book book1 = new Book();
  2. book1.id = 901;
  3. book1.productCategory = "Book";
  4. book1.title = "Book 901 Title";
  5. Book book2 = new Book();
  6. book2.id = 902;
  7. book2.productCategory = "Book";
  8. book2.title = "Book 902 Title";
  9. mapper.batchSave(Arrays.asList(book1, book2));
  • batchWrite
    • 使用对AmazonDynamoDB.batchWriteItem方法的一个或多个调用,将对象保存到一个或多个表并从一个或多个表中删除对象。
  1. // Create a Forum item to save
  2. Forum forumItem = new Forum();
  3. forumItem.name = "Test BatchWrite Forum";
  4. // Create a Thread item to save
  5. Thread threadItem = new Thread();
  6. threadItem.forumName = "AmazonDynamoDB";
  7. threadItem.subject = "My sample question";
  8. // Load a ProductCatalog item to delete
  9. Book book3 = mapper.load(Book.class, 903);
  10. List<Object> objectsToWrite = Arrays.asList(forumItem, threadItem);
  11. List<Book> objectsToDelete = Arrays.asList(book3);
  12. mapper.batchWrite(objectsToWrite, objectsToDelete);
  • 计数
    • 计算指定的扫描表达式并返回匹配项的计数。
  • marshallIntoObject
    • 一种将结果从低级API转换为域对象的实用程序方法。

支持的数据类型

Amazon DynamoDB支持以下原始数据类型和原始包装器类。

  • 布尔,布尔
  • 字节,字节
  • 日期(作为ISO8601毫秒精度的字符串,转换为UTC)
  • 日历(作为ISO8601毫秒精度的字符串,已转换为UTC)
  • 好久好久
  • 整数,整数
  • 双倍
  • 浮,浮
  • 大十进制
  • 大整数

Amazon DynamoDB支持Java Set集合类型。 如果映射的集合属性不是Set,则将引发异常。

下表总结了前面的Java类型如何映射到Amazon DynamoDB类型。

Java类型Amazon DynamoDB类型
所有数字类型N(数字类型)
弦乐S(字符串类型)
布尔值N(数字类型),0或1。
字节缓冲区B(二进制类型)
日期S(字符串类型)。 日期值存储为ISO-8601格式的字符串。
设置收集类型SS(字符串集)类型,NS(数字集)类型或BS(二进制集)类型。

Java示例:CRUD操作

CatalogItem.java

  1. import java.util.Set;
  2. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
  3. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
  4. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
  5. @DynamoDBTable(tableName="ProductCatalog")
  6. public class CatalogItem {
  7. private Integer id;
  8. private String title;
  9. private String ISBN;
  10. private Set bookAuthors;
  11. @DynamoDBHashKey(attributeName="Id")
  12. public Integer getId() { return id; }
  13. public void setId(Integer id) { this.id = id; }
  14. @DynamoDBAttribute(attributeName="Title")
  15. public String getTitle() { return title; }
  16. public void setTitle(String title) { this.title = title; }
  17. @DynamoDBAttribute(attributeName="ISBN")
  18. public String getISBN() { return ISBN; }
  19. public void setISBN(String ISBN) { this.ISBN = ISBN;}
  20. @DynamoDBAttribute(attributeName = "Authors")
  21. public Set getBookAuthors() { return bookAuthors; }
  22. public void setBookAuthors(Set bookAuthors) { this.bookAuthors = bookAuthors; }
  23. @Override
  24. public String toString() {
  25. return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors
  26. + ", id=" + id + ", title=" + title + "]";
  27. }
  28. }

ObjectPersistenceCRUDExample.java

  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import java.util.Random;
  7. import com.amazonaws.AmazonClientException;
  8. import com.amazonaws.AmazonServiceException;
  9. import com.amazonaws.auth.AWSCredentials;
  10. import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
  11. import com.amazonaws.regions.Region;
  12. import com.amazonaws.regions.Regions;
  13. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
  14. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
  15. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
  16. import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
  17. import com.amazonaws.services.dynamodbv2.model.AttributeValue;
  18. import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
  19. import com.amazonaws.services.dynamodbv2.model.Condition;
  20. import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
  21. import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
  22. import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
  23. import com.amazonaws.services.dynamodbv2.model.KeyType;
  24. import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
  25. import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
  26. import com.amazonaws.services.dynamodbv2.model.ScanRequest;
  27. import com.amazonaws.services.dynamodbv2.model.ScanResult;
  28. import com.amazonaws.services.dynamodbv2.model.TableDescription;
  29. import com.amazonaws.services.dynamodbv2.model.TableStatus;
  30. public class ObjectPersistenceCRUDExample {
  31. static AmazonDynamoDBClient client;
  32. private DynamoDBMapper mapper;
  33. private static int PRODUCT_ID;
  34. public static void main(String[] args) throws IOException {
  35. ObjectPersistenceCRUDExample demo = new ObjectPersistenceCRUDExample();
  36. demo.init();
  37. demo.createTable("ProductCatalog");
  38. for (int i = ; i < 100; i++) {
  39. System.out.println(i);
  40. PRODUCT_ID = PRODUCT_ID + i;
  41. demo.insert();
  42. }
  43. demo.getAllRows();
  44. CatalogItem itemRetrieved = demo.load(PRODUCT_ID);
  45. demo.update(itemRetrieved);
  46. CatalogItem updatedItem = demo.load(PRODUCT_ID);
  47. demo.delete(updatedItem);
  48. demo.load(updatedItem.getId());
  49. System.out.println("Example complete!");
  50. }
  51. private void init() {
  52. PRODUCT_ID = new Random().nextInt(1000);
  53. AWSCredentials credentials = new ClasspathPropertiesFileCredentialsProvider()
  54. .getCredentials();
  55. client = new AmazonDynamoDBClient(credentials);
  56. Region usWest2 = Region.getRegion(Regions.US_WEST_2);
  57. client.setRegion(usWest2);
  58. mapper = new DynamoDBMapper(client);
  59. }
  60. private void createTable(String tableName) {
  61. try {
  62. CreateTableRequest createTableRequest = new CreateTableRequest()
  63. .withTableName(tableName);
  64. createTableRequest.withKeySchema(new KeySchemaElement()
  65. .withAttributeName("Id").withKeyType(KeyType.HASH));
  66. createTableRequest
  67. .withAttributeDefinitions(new AttributeDefinition()
  68. .withAttributeName("Id").withAttributeType(
  69. ScalarAttributeType.N));
  70. createTableRequest
  71. .withProvisionedThroughput(new ProvisionedThroughput()
  72. .withReadCapacityUnits(10L).withWriteCapacityUnits(
  73. 10L));
  74. TableDescription createdTableDescription = client.createTable(
  75. createTableRequest).getTableDescription();
  76. System.out.println("Created Table: " + createdTableDescription);
  77. // Wait for it to become active
  78. waitForTableToBecomeAvailable(tableName);
  79. } catch (AmazonServiceException e) {
  80. e.printStackTrace();
  81. } catch (AmazonClientException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. private void waitForTableToBecomeAvailable(String tableName) {
  86. System.out.println("Waiting for " + tableName + " to become ACTIVE...");
  87. long startTime = System.currentTimeMillis();
  88. long endTime = startTime + (10 * 60 * 1000);
  89. while (System.currentTimeMillis() < endTime) {
  90. try {
  91. Thread.sleep(1000 * 20);
  92. } catch (Exception e) {
  93. }
  94. try {
  95. DescribeTableRequest request = new DescribeTableRequest()
  96. .withTableName(tableName);
  97. TableDescription tableDescription = client.describeTable(
  98. request).getTable();
  99. String tableStatus = tableDescription.getTableStatus();
  100. System.out.println(" - current state: " + tableStatus);
  101. if (tableStatus.equals(TableStatus.ACTIVE.toString()))
  102. return;
  103. } catch (AmazonServiceException ase) {
  104. if (ase.getErrorCode().equalsIgnoreCase(
  105. "ResourceNotFoundException") == false)
  106. throw ase;
  107. }
  108. }
  109. throw new RuntimeException("Table " + tableName + " never went active");
  110. }
  111. private void insert() {
  112. CatalogItem item = new CatalogItem();
  113. item.setId(PRODUCT_ID);
  114. item.setTitle("Book PRODUCT_ID");
  115. item.setISBN("611-1111111111");
  116. item.setBookAuthors(new HashSet(Arrays.asList("Author1",
  117. "Author2")));
  118. // Save the item (book).
  119. mapper.save(item);
  120. }
  121. private void update(CatalogItem itemRetrieved) {
  122. itemRetrieved.setISBN("622-2222222222");
  123. itemRetrieved.setBookAuthors(new HashSet(Arrays.asList(
  124. "Author1", "Author3")));
  125. mapper.save(itemRetrieved);
  126. System.out.println("Item updated:");
  127. System.out.println(itemRetrieved);
  128. }
  129. private void delete(CatalogItem updatedItem) {
  130. // Delete the item.
  131. mapper.delete(updatedItem);
  132. }
  133. private CatalogItem load(int id) {
  134. // Retrieve the updated item.
  135. DynamoDBMapperConfig config = new DynamoDBMapperConfig(
  136. DynamoDBMapperConfig.ConsistentReads.CONSISTENT);
  137. CatalogItem updatedItem = mapper.load(CatalogItem.class, id, config);
  138. if (updatedItem == null) {
  139. System.out.println("Done - Sample item is deleted.");
  140. } else {
  141. System.out.println("Retrieved item:");
  142. System.out.println(updatedItem);
  143. }
  144. return updatedItem;
  145. }
  146. private void getAllRows() {
  147. ScanRequest scanRequest = new ScanRequest()
  148. .withTableName("ProductCatalog");
  149. scanRequest.setLimit(10);
  150. HashMap scanFilter = new HashMap();
  151. Condition condition = new Condition().withComparisonOperator(
  152. ComparisonOperator.EQ.toString()).withAttributeValueList(
  153. new AttributeValue().withS("611-1111111111"));
  154. scanFilter.put("ISBN", condition);
  155. Condition condition2 = new Condition().withComparisonOperator(
  156. ComparisonOperator.LE.toString()).withAttributeValueList(
  157. new AttributeValue().withN("1000"));
  158. scanFilter.put("Id", condition2);
  159. scanRequest.withScanFilter(scanFilter);
  160. try {
  161. System.out.println("Scan Request: " + scanRequest);
  162. ScanResult scanResponse = client.scan(scanRequest);
  163. for (Map item : scanResponse.getItems()) {
  164. System.out.println(item.get("Id").getN() + " , " +
  165. item.get("ISBN").getS() + " , " +
  166. item.get("Authors").getSS() + " , " +
  167. item.get("Title").getS());
  168. }
  169. System.out.println("Scan Response: " + scanResponse);
  170. System.out.println("Count: " + scanResponse.getCount());
  171. System.out.println("Scanned Count: "
  172. + scanResponse.getScannedCount());
  173. System.out.println("Items: " + scanResponse.getItems());
  174. } catch (AmazonServiceException e) {
  175. e.printStackTrace();
  176. } catch (AmazonClientException e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. }

分享好友

分享这个小栈给你的朋友们,一起进步吧。

Amazon DynamoDB
创建时间:2022-04-08 14:56:47
Amazon DynamoDB
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • LCR_
    专家
戳我,来吐槽~