Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Lavora con classi di dati immutabili
La funzionalità di mappatura del DynamoDB Enhanced API Client funziona con classi di dati immutabili. Una classe immutabile ha solo getter e richiede una classe builder che viene utilizzata per creare istanze della classe. SDK Invece di utilizzare l'@DynamoDbBean
annotazione come mostrato nella classe Customer, le classi immutabili utilizzano l'@DynamoDbImmutable
annotazione, che accetta un parametro che indica la classe builder da utilizzare.
La classe seguente è una versione immutabile di. Customer
package org.example.tests.model.immutable; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbImmutable; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondarySortKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; import java.time.Instant; @DynamoDbImmutable(builder = CustomerImmutable.Builder.class) public class CustomerImmutable { private final String id; private final String name; private final String email; private final Instant regDate; private CustomerImmutable(Builder b) { this.id = b.id; this.email = b.email; this.name = b.name; this.regDate = b.regDate; } // This method will be automatically discovered and used by the TableSchema. public static Builder builder() { return new Builder(); } @DynamoDbPartitionKey public String id() { return this.id; } @DynamoDbSortKey public String email() { return this.email; } @DynamoDbSecondaryPartitionKey(indexNames = "customers_by_name") public String name() { return this.name; } @DynamoDbSecondarySortKey(indexNames = {"customers_by_date", "customers_by_name"}) public Instant regDate() { return this.regDate; } public static final class Builder { private String id; private String email; private String name; private Instant regDate; // The private Builder constructor is visible to the enclosing CustomerImmutable class. private Builder() {} public Builder id(String id) { this.id = id; return this; } public Builder email(String email) { this.email = email; return this; } public Builder name(String name) { this.name = name; return this; } public Builder regDate(Instant regDate) { this.regDate = regDate; return this; } // This method will be automatically discovered and used by the TableSchema. public CustomerImmutable build() { return new CustomerImmutable(this); } } }
È necessario soddisfare i seguenti requisiti quando si annota una classe di dati con. @DynamoDbImmutable
-
Ogni metodo che non sostituisce
Object.class
e con cui non è stato annotato@DynamoDbIgnore
deve essere un getter per un attributo della tabella DynamoDB. -
Ogni getter deve avere un setter corrispondente con distinzione tra maiuscole e minuscole nella classe builder.
-
Deve essere soddisfatta solo una delle seguenti condizioni di costruzione.
-
La classe builder deve avere un costruttore pubblico predefinito.
-
La classe di dati deve avere un metodo statico pubblico denominato
builder()
che non accetta parametri e restituisce un'istanza della classe builder. Questa opzione è mostrata nella classe immutableCustomer
.
-
-
La classe builder deve avere un metodo pubblico denominato
build()
che non accetta parametri e restituisce un'istanza della classe immutabile.
Per crearne uno TableSchema
per la tua classe immutabile, usa il fromImmutableClass()
metodo on TableSchema
come mostrato nel frammento seguente.
static final TableSchema<CustomerImmutable> customerImmutableTableSchema = TableSchema.fromImmutableClass(CustomerImmutable.class);
Proprio come è possibile creare una tabella DynamoDB da una classe mutabile, è possibile crearne una da una classe immutabile con una chiamata singola a of, come illustrato nel seguente esempio DynamoDbTable
di createTable()
snippet.
static void createTableFromImmutable(DynamoDbEnhancedClient enhancedClient, String tableName, DynamoDbWaiter waiter){ // First, create an in-memory representation of the table using the 'table()' method of the DynamoDb Enhanced Client. // 'table()' accepts a name for the table and a TableSchema instance that you created previously. DynamoDbTable<CustomerImmutable> customerDynamoDbTable = enhancedClient .table(tableName, TableSchema.fromImmutableClass(CustomerImmutable.class)); // Second, call the 'createTable()' method on the DynamoDbTable instance. customerDynamoDbTable.createTable(); waiter.waitUntilTableExists(b -> b.tableName(tableName)); }
Usa librerie di terze parti, come Lombok
Le librerie di terze parti, come Project Lombok
L'esempio seguente mostra la classe immutabile CustomerImmutable
con annotazioni Lombok. Nota come la onMethod
funzionalità di Lombok copia le annotazioni DynamoDB basate su attributi, ad esempio, sul codice generato. @DynamoDbPartitionKey
@Value @Builder @DynamoDbImmutable(builder = Customer.CustomerBuilder.class) public class Customer { @Getter(onMethod_=@DynamoDbPartitionKey) private String id; @Getter(onMethod_=@DynamoDbSortKey) private String email; @Getter(onMethod_=@DynamoDbSecondaryPartitionKey(indexNames = "customers_by_name")) private String name; @Getter(onMethod_=@DynamoDbSecondarySortKey(indexNames = {"customers_by_date", "customers_by_name"})) private Instant createdDate; }