本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
變更屬性的更新行為
您可以在執行更新操作時自訂個別屬性的更新行為。DynamoDB 增強型用戶端中的更新操作範例API為 updateItem()
例如,假設您想要在記錄上儲存在時間戳記上建立的 。不過,只有當資料庫中沒有屬性的現有值時,才希望寫入其值。在此情況下,您可以使用WRITE_IF_NOT_EXISTS
更新行為。
下列範例顯示將行為新增至 createdOn
屬性的註釋。
@DynamoDbBean public class Customer extends GenericRecord { private String id; private Instant createdOn; @DynamoDbPartitionKey public String getId() { return this.id; } public void setId(String id) { this.name = id; } @DynamoDbUpdateBehavior(UpdateBehavior.WRITE_IF_NOT_EXISTS) public Instant getCreatedOn() { return this.createdOn; } public void setCreatedOn(Instant createdOn) { this.createdOn = createdOn; } }
您可以在建置靜態資料表結構描述時宣告相同的更新行為,如以下備註行 1 之後的範例所示。
static final TableSchema<Customer> CUSTOMER_TABLE_SCHEMA = TableSchema.builder(Customer.class) .newItemSupplier(Customer::new) .addAttribute(String.class, a -> a.name("id") .getter(Customer::getId) .setter(Customer::setId) .tags(StaticAttributeTags.primaryPartitionKey())) .addAttribute(Instant.class, a -> a.name("createdOn") .getter(Customer::getCreatedOn) .setter(Customer::setCreatedOn) // 1. Add an UpdateBehavior. .tags(StaticAttributeTags.updateBehavior(UpdateBehavior.WRITE_IF_NOT_EXISTS))) .build();