dynamodb.adoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. = DynamoDB Integration
  2. https://aws.amazon.com/dynamodb/[DynamoDB] is a fully managed serverless key/value Nosql database designed for high performance.
  3. A Spring Boot starter is provided to auto-configure DynamoDb integration beans.
  4. Maven coordinates, using <<index.adoc#bill-of-materials, Spring Cloud AWS BOM>>:
  5. [source,xml]
  6. ----
  7. <dependency>
  8. <groupId>io.awspring.cloud</groupId>
  9. <artifactId>spring-cloud-aws-starter-dynamodb</artifactId>
  10. </dependency>
  11. ----
  12. DynamoDB integration will only provide autoconfiguration and simple `DynamoDbOperations` which can be used to communicate with DynamoDb, build repositories and so on...
  13. == DynamoDbOperations
  14. The starter automatically configures and registers a `DynamoDbOperations` bean providing higher level abstractions for working with DynamoDb.
  15. `DynamoDbTemplate` - a default `DynamoDbOperations` implementation - being built on top of `DynamoDbEnhancedClient` uses annotations provided by AWS.
  16. The list of supported annotations can be found https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/mapper/annotations/package-summary.html[here].
  17. `DynamoDbEnchancedClient` supports a programming model similar to JPA - where a class is turned into an entity through applying certain annotations:
  18. [source,java]
  19. ----
  20. import java.util.UUID;
  21. import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
  22. import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
  23. @DynamoDbBean
  24. public class Person {
  25. private UUID id;
  26. private String name;
  27. private String lastName;
  28. @DynamoDbPartitionKey
  29. public UUID getId() {
  30. return id;
  31. }
  32. ...
  33. }
  34. ----
  35. `DynamoDbTemplate` provides methods to perform typical CRUD operations on such entities, plus it adds convenience methods for querying DynamoDb:
  36. [source,java]
  37. ----
  38. import io.awspring.cloud.dynamodb.DynamoDbTemplate;
  39. ...
  40. @Autowired
  41. DynamoDbTemplate dynamoDbTemplate;
  42. ...
  43. Person person = new Person(...)
  44. dynamoDbTemplate.save(person);
  45. ----
  46. === Resolving Table Name
  47. To resolve a table name for an entity, `DynamoDbTemplate` uses a bean of type `DynamoDbTableNameResolver`. The default implementation turns an entity class name into its snake case representation with the possibility of using a table name prefix (optional). Specify the `spring.cloud.aws.dynamodb.table-prefix` to provide a table name prefix. The prefix is prepended to the table name. For example, if `spring.cloud.aws.dynamodb.table-prefix` is configured as `foo_` and the entity class is `Person`, then the default implementation resolves the table name as `foo_person`. However if you do not specify `spring.cloud.aws.dynamodb.table-prefix`, the table name will be resolved as `person`.
  48. To use a custom implementation, declare a bean of type `DynamoDbTableNameResolver` and it will get injected into `DynamoDbTemplate` automatically during auto-configuration.
  49. === Resolving Table Schema
  50. To resolve a table schema for an entity, `DynamoDbTemplate` uses a bean of type `DynamoDbTableSchemaResolver`. The default implementation caches `TableSchema` objects in internal map.
  51. To use custom implementation, declare a bean of type `DynamoDbTableSchemaResolver` and it will get injected into `DynamoDbTemplate` automatically during auto-configuration.
  52. To register a custom table schema for a DynamoDB entity a bean of type `TableSchema` should be created:
  53. [source, java]
  54. ----
  55. @Configuration
  56. public class MyTableSchemaConfiguration {
  57. @Bean
  58. public TableSchema<MyEntity> myEntityTableSchema() {
  59. // create and return a TableSchema object for the MyEntity class
  60. }
  61. }
  62. ----
  63. IMPORTANT: Because of classloader related https://github.com/aws/aws-sdk-java-v2/issues/2604[issue] in AWS SDK DynamoDB Enhanced client, to use Spring Cloud AWS DynamoDB module together with Spring Boot DevTools you must create a custom table schema resolver and define schema using `StaticTableSchema`.
  64. == Using DynamoDb Client
  65. Autoconfiguration automatically configures `DynamoDbClient` which can be used for low level api calls and `DynamoDbEnhancedClient` if `DynamoDbOperations` are not enough.
  66. If autoconfigured `DynamoDbClient` or `DynamoDbEnhancedClient` bean configuration does not meet your needs, it can be replaced by creating your custom bean.
  67. [source,java]
  68. ----
  69. import java.util.Collections;
  70. import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
  71. import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
  72. import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
  73. public class DynamoDbService {
  74. private final DynamoDbClient dynamoDbClient;
  75. public DynamoDbService(DynamoDbClient dynamoDbClient) {
  76. this.dynamoDbClient = dynamoDbClient;
  77. }
  78. void deletePerson(String uuid) {
  79. dynamoDbClient.deleteItem(DeleteItemRequest.builder().key(Collections.singletonMap("uuid", AttributeValue.builder().s(uuid).build())).build());
  80. }
  81. }
  82. ----
  83. == Using DynamoDB Accelerator
  84. The starter automatically configures and registers an `software.amazon.dax.ClusterDaxClient` bean if it finds the following is added to the project:
  85. [source,xml]
  86. ----
  87. <dependency>
  88. <groupId>software.amazon.dax</groupId>
  89. <artifactId>amazon-dax-client</artifactId>
  90. </dependency>
  91. ----
  92. == Configuration
  93. The Spring Boot Starter for DynamoDb provides the following configuration options:
  94. [cols="3,3,1,1"]
  95. |===
  96. | Name | Description | Required | Default value
  97. | `spring.cloud.aws.dynamodb.enabled` | Enables the DynamoDb integration. | No | `true`
  98. | `spring.cloud.aws.dynamodb.endpoint` | Configures endpoint used by `DynamoDbClient`. | No |
  99. | `spring.cloud.aws.dynamodb.region` | Configures region used by `DynamoDbClient`. | No |
  100. | `spring.cloud.aws.dynamodb.table-prefix` | Table name prefix used by the default `DynamoDbTableNameResolver` implementation. | No |
  101. | `spring.cloud.aws.dynamodb.dax.idle-timeout-millis` |Timeout for idle connections with the DAX cluster. | No | `30000`
  102. | `spring.cloud.aws.dynamodb.dax.url` | DAX cluster endpoint. | Yes |
  103. | `spring.cloud.aws.dynamodb.dax.connection-ttl-millis` | Connection time to live. | No | `0`
  104. | `spring.cloud.aws.dynamodb.dax.connect-timeout-millis` | Connection timeout | No | `1000`
  105. | `spring.cloud.aws.dynamodb.dax.request-timeout-millis` | Request timeout for connections with the DAX cluster. | No | `1000`
  106. | `spring.cloud.aws.dynamodb.dax.write-retries` | Number of times to retry writes, initial try is not counted. | No | `2`
  107. | `spring.cloud.aws.dynamodb.dax.read-retries` | Number of times to retry reads, initial try is not counted. | No | `2`
  108. | `spring.cloud.aws.dynamodb.dax.cluster-update-interval-millis` | Interval between polling of cluster members for membership changes. | No | `4000`
  109. | `spring.cloud.aws.dynamodb.dax.endpoint-refresh-timeout-millis` | Timeout for endpoint refresh. | No | `6000`
  110. | `spring.cloud.aws.dynamodb.dax.max-concurrency` | Maximum number of concurrent requests. | No | 1000
  111. | `spring.cloud.aws.dynamodb.dax.max-pending-connection-acquires` | Maximum number of pending Connections to acquire. | No | 10000
  112. | `spring.cloud.aws.dynamodb.dax.skip-host-name-verification` | Skips hostname verification in url. | No |
  113. |===
  114. == IAM Permissions
  115. Since it depends on how you will use DynamoDb integration providing a list of IAM policies would be pointless since least privilege model should be used.
  116. To check what IAM policies DynamoDb uses and see which ones you should use please check https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/using-identity-based-policies.html[IAM policies]