英文:
DynamoDBMapper scanPage returns no results
问题
以下是翻译好的内容:
当我尝试从DynamoDB表中获取分页数据集时,如果所寻找的数据位于表的顶部,它能够完美地工作。但如果数据位于更深的位置(我已经按用户别名进行了排序,每个别名有100个元素),则返回空。
AmazonDynamoDB dynamo = AmazonDynamoDBClientBuilder.standard()
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
"XXXXXXX",
"XXXXXXX")))
.build();
DynamoDBMapper mapper = new DynamoDBMapper(dynamo);
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":user", new AttributeValue().withS(request.getUser().getAlias()));
ScanResultPage<DatabaseFeedObject> feedEntry;
feedEntry = mapper.scanPage(
DatabaseFeedObject.class,
new DynamoDBScanExpression()
.withFilterExpression("user_alias = :user")
.withLimit(request.getLimit())
.withExpressionAttributeValues(expressionAttributeValues));
// feedEntry.getResults()返回为空,
// 但feedEntry.getLastEvaluatedKey()返回一个
// 与原始查询不匹配的表中元素。
我最初的想法是一直运行查询,直到达到所需的限制,但这会很快变得低效且昂贵。有没有其他方法可以做到这一点?
英文:
When I try to get a paginated set of data from my DynamoDB table, it works perfectly, but only if the data it's looking for is at the top of the table. If it's deeper down (I have the data sorted by user_alias, with 100 elements for each alias), it returns nothing.
AmazonDynamoDB dynamo = AmazonDynamoDBClientBuilder.standard()
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
"XXXXXXX",
"XXXXXXX")))
.build();
DynamoDBMapper mapper = new DynamoDBMapper(dynamo);
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":user", new AttributeValue().withS(request.getUser().getAlias()));
ScanResultPage<DatabaseFeedObject> feedEntry;
feedEntry = mapper.scanPage(
DatabaseFeedObject.class,
new DynamoDBScanExpression()
.withFilterExpression("user_alias = :user")
.withLimit(request.getLimit())
.withExpressionAttributeValues(expressionAttributeValues));
//feedEntry.getResults() returns nothing,
//but feedEntry.getLastEvaluatedKey() returns an
//element in the table that does not fit the original query.
My first thought was to just keep running the query until I hit the limit I want, but that will quickly become inefficient and costly. Is there any other way to do it?
答案1
得分: 0
我最近发现了"Scan"和"Query"之间的区别。"Scan"将简单地遍历整个表,而"Query"将使用类似SQL的行为来查找您所需的内容。我迁移到了"mapper.queryPage()",它运行得很好。实际上,甚至比以前还要快。我将保持"已接受的答案"未被选中,以便如果有人想提供另一个答案,尽管提供吧。
英文:
I have discovered recently the difference between Scan and Query. Scan will simply parse through the entire table, while query will use a SQL like behavior to find what you need. I migrated to mapper.queryPage() instead, and it works great. Even faster than before actually. I'll leave the "accepted answer" unselected, so if anyone wants to provide another answer, be my guest.
专注分享java语言的经验与见解,让所有开发者获益!
评论