更改属性的更新行为 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

更改属性的更新行为

在执行更新 操作时,您可以自定义各个属性的更新行为。DynamoDB 增强型客户端 API 中的一些更新操作示例如下 updateItem () 和 ()。transactWriteItems

例如,假设您要在记录中存储 created on 时间戳。但是,您希望仅在数据库中没有该属性的现有值时才写入其值。在这种情况下,您可以使用 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();