使用不可變的資料類別 - AWS SDK for Java 2.x

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

使用不可變的資料類別

DynamoDB 增強型用戶端 API 的對應功能可與不可變的資料類別搭配使用。不可變類只有 getter,並且需要 SDK 用於創建類實例的構建器類。不可變類別不會使用 Custo mer 類別中所示的@DynamoDbBean註解,而是使用@DynamoDbImmutable註解,註解會接受指示要使用的建構器類別的參數。

下列類別是的不可變版本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); } } }

當您使用註解資料類別時,您必須符合下列需求@DynamoDbImmutable

  1. 每個不是覆寫Object.class且未被註解的方法,都@DynamoDbIgnore必須是 DynamoDB 表格屬性的吸引器。

  2. 每個 getter 必須在構建器類上具有相應的區分大小寫的 setter。

  3. 只能滿足下列其中一個施工條件。

    • 構建器類必須有一個公共默認構造函數。

    • 資料類別必須有名為的公用靜態方法,builder()該方法不接受任何參數,並傳回建置器類別的實體。這個選項顯示在不可變的Customer類。

  4. 建構器類別必須具有名為的公用方法,build()該方法不接受任何參數,並傳回不可變類別的執行個體。

TableSchema為您的不可變類創建一個,請使用上的fromImmutableClass()方法,TableSchema如下面的代碼片段。

static final TableSchema<CustomerImmutable> customerImmutableTableSchema = TableSchema.fromImmutableClass(CustomerImmutable.class);

正如您可以從可變類別建立 DynamoDB 資料表一樣,您可以透過一次性呼叫 of 從不可變類別建立一個資料表,如下列程式碼片段createTable()DynamoDbTable例所示。

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)); }

使用第三方庫,例如龍目島

協力廠商程式庫,例如 Project Log,可協助產生與不可變物件相關聯的樣板程式碼。只要資料類別遵循本節中詳述的慣例,DynamoDB 增強型用戶端 API 就可與這些程式庫搭配使用。

下面的例子顯示了龍目島註釋不可變的CustomerImmutable類。請注意,龍目島的onMethod功能如何將以屬性為基礎的 DynamoDB 註釋 (例如@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; }