You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DynamoDBMapper的query方法查询GSI无匹配值时返回什么?

关于DynamoDBMapper.query方法的适用场景与无匹配返回值

1. 是否适用于GSI查询

完全适用。只要在你的实体类上通过@DynamoDBIndexHashKey(指定GSI哈希键)和@DynamoDBIndexRangeKey(若GSI包含范围键则添加)注解声明GSI的键结构,再构造DynamoDBQueryExpression时指定索引名称与查询条件,就能针对GSI执行精准查询。

2. 无匹配值时的返回结果

当查询没有匹配到任何项时,query方法会返回一个空的List集合(非null)。你可以通过调用集合的isEmpty()方法判断是否存在匹配数据,进而决定是否抛出错误。

示例代码

// 假设实体类User已通过注解声明GSI:gsi_email_index(哈希键为email)
DynamoDBQueryExpression<User> queryExpression = new DynamoDBQueryExpression<User>()
    .withIndexName("gsi_email_index")
    .withHashKeyValues(new User().setEmail("target@example.com"))
    .withConsistentRead(false); // GSI仅支持最终一致性读取,必须设为false

List<User> queryResults = dynamoDBMapper.query(User.class, queryExpression);

if (queryResults.isEmpty()) {
    throw new IllegalArgumentException("未找到匹配的GSI值");
} else {
    // 处理查询到的结果集
}

额外注意事项

  • GSI查询不支持强一致性读取,必须将withConsistentRead设置为false,否则会抛出异常。
  • 若GSI包含范围键,可通过withRangeKeyCondition添加范围过滤条件,进一步缩小查询范围。

内容的提问来源于stack exchange,提问作者Liumx31

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:30:44