DynamoDBMapper 提供了从表中读取一项的不同方法:
有推荐吗,该使用哪一个?在快速测试中,以下两个代码片段对于主键 = 哈希和范围键 = 日期的表返回相同的“MyEntry”项,而查询方法大约快 10%。
load
public MyEntry getEntryForDay(final Integer hash, final LocalDate date) {
return mapper.load(MyEntry.class, hash, date);
}
query
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);
}
加载和查询是不同的操作:
如果您只有哈希键架构,它们会执行相同的操作 - 检索具有指定哈希键的项目。
如果您有哈希范围架构,则加载会检索由单个哈希+范围对标识的特定项目。查询检索具有指定哈希键并满足范围键条件的所有项目。
由于您对哈希键和范围键都使用相等运算符,因此操作完全相同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)