DynamoDBMapper提供了从表中读取一个项目的不同方法:
是否有推荐,哪些可以使用?在快速测试中,以下两个代码段为具有主键= hash和range key = date的表返回相同的"MyEntry"项,而查询方法大约快10%.
加载
public MyEntry getEntryForDay(final Integer hash, final LocalDate date) {
return mapper.load(MyEntry.class, hash, date);
}
Run Code Online (Sandbox Code Playgroud)
询问
public MyEntry getEntryForDay(final Integer hash, final LocalDate date) {
final MyEntry hashKeyValues = new MyEntry ();
hashKeyValues.setHash(hash);
final Condition rangeKeyCondition = new Condition()//
.withComparisonOperator(ComparisonOperator.EQ.toString())//
.withAttributeValueList(new AttributeValue().withS(new LocalDateMarshaller().marshall(date)));
final DynamoDBQueryExpression<MyEntry> queryExpression = new DynamoDBQueryExpression<MyEntry>()//
.withHashKeyValues(hashKeyValues)//
.withRangeKeyCondition("date", rangeKeyCondition)//
.withLimit(1);
final List<MyEntry> storedEntries = mapper
.query(MyEntry.class, queryExpression);
if (storedEntries.size() == 0) {
return null;
}
return storedEntries.get(0);
}
Run Code Online (Sandbox Code Playgroud)