DynamoDB增强客户端间歇性不保存对象



我正在保存java对象使用dynamodbenhandedclient和它的保存对象有时,有时它不,同一类的实例,相同的DynamoDB记录。

我没有看到客户端API抛出异常,所以我不知道如何调试。

在什么情况下客户端API或DynamoDB会丢弃请求?

已发放读容量单位2 (Auto Scaling Enabled)
已发放写容量单位3 (Auto Scaling Enabled)

public static final TableSchema<School> TABLE_SCHEMA = StaticTableSchema
.builder(School.class).newItemSupplier(School::new)
.addAttribute(String.class,
a -> a.name("Id").getter(School::getId).setter(School::setId)
.tags(primaryPartitionKey()))
.addAttribute(Boolean.class,
a -> a.name("enabled").getter(School::isEnabled)
.setter(School::setEnabled))
.addAttribute(String.class,
a -> a.name("name").getter(School::getName).setter(School::setName))
.addAttribute(String.class,
a -> a.name("dateTime").getter(School::getDateTime)
.setter(School::setDateTime))
.addAttribute(Integer.class,
a -> a.name("attMode").getter(School::getAttMode)
.setter(School::setAttMode))
.addAttribute(Integer.class,
a -> a.name("attOcc").getter(School::getAttOcc)
.setter(School::setAttOcc))
.addAttribute(EnhancedType.documentOf(Sms.class,Sms.SMS_SCHEMA)  ,
a -> a.name("sms").getter(School::getSms).setter(School::setSms) )
.addAttribute(EnhancedType.listOf(EnhancedType.documentOf(Role.class,Role.ROLE_SCHEMA)  ),
a -> a.name("rol").getter(School::getRol).setter(School::setRol))

//***Room will need to be defined statically as it has nested documents
.addAttribute(EnhancedType.listOf(EnhancedType.documentOf(Room.class,Room.ROOM_SCHEMA) ) ,
a -> a.name("rooms").getter(School::getRooms).setter(School::setRooms))


.addAttribute(String.class,
a -> a.name("notes").getter(School::getNotes)
.setter(School::setNotes))
//Since forum has nested list we may need to define static table schema
.addAttribute(EnhancedType.listOf(EnhancedType.documentOf(Forum.class,Forum.Forum_SCHEMA)  ) ,
a -> a.name("forums").getter(School::getForums)
.setter(School::setForums))
//since subscription does not have nested document we can just do TableSchema.fromClass(Subsription.class) 
.addAttribute(  EnhancedType.listOf(EnhancedType.documentOf(SchoolSubsription.class, SchoolSubsription.SUBSCRIPTION_SCHEMA )  )     ,
a -> a.name("subscription").getter(School::getSubscriptions)
.setter(School::setSubscriptions)
)
.build();

下面的log4j config不记录请求到dynamo。

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/blank-java/src/main/resources/log4j2.xml -->
<Configuration status="WARN">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Lambda" />
</Root>

<Logger name="software.amazon.awssdk" level="TRACE" />
<Logger name="software.amazon.awssdk.request" level="DEBUG" />
</Loggers>
</Configuration>

如果您正在使用增强型客户端-您必须使用POJO类使用@DynamoDbBean注释。此外,这段代码看起来也不像增强型客户机代码。例如,您正在使用addAttribute,这不是使用增强型客户机的编码模式。更多信息在这里-映射DynamoDB表项。

使用增强客户端需要调用enhancedClient.table()呼叫然后创建一个Object,设置数据,并调用putItem方法——如下所示。

public static void putRecord(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
// Create an Instant
LocalDate localDate = LocalDate.parse("2020-04-07");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// Populate the Table
Customer custRecord = new Customer();
custRecord.setCustName("Susan red");
custRecord.setId("id146");
custRecord.setEmail("sred@noserver.com");
custRecord.setRegistrationDate(instant) ;
// Put the customer data into a DynamoDB table
custTable.putItem(custRecord);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("done");
} 

最新更新