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

如何在DynamoDB中查询近15分钟内的所有数据(Java实现)

DynamoDB查询创建时间超过15分钟的数据(基于GSI)

从RDBMS转DynamoDB确实得适应它的查询逻辑,不过你的GSI设计刚好适配这个需求——哈希键materialType+排序键createTime(毫秒时间戳),咱们可以用Query操作高效完成过滤,比Scan靠谱多了。

核心逻辑

要筛选创建时间超过15分钟的数据,本质是找createTime小于等于当前时间减15分钟的记录。借助GSI的排序键特性,我们可以直接在Query中指定范围条件,避免全表扫描。

Java代码示例(AWS SDK v2,官方推荐版本)

首先确保引入SDK依赖(Maven为例):

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>dynamodb</artifactId>
    <version>2.25.0</version> <!-- 替换为最新稳定版 -->
</dependency>

完整查询代码:

import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

public class OldMaterialQuery {
    public static void main(String[] args) {
        // 初始化DynamoDB客户端,替换为你的区域
        Region region = Region.US_EAST_1;
        DynamoDbClient ddbClient = DynamoDbClient.builder()
                .region(region)
                .credentialsProvider(DefaultCredentialsProvider.create())
                .build();

        // 计算15分钟前的毫秒时间戳
        long cutoffTimestamp = Instant.now().minus(15, ChronoUnit.MINUTES).toEpochMilli();
        
        // 替换为你的实际参数
        String targetMaterialType = "plastic";
        String gsiName = "materialType-createTime-index";
        String tableName = "materials";

        // 构建查询参数
        Map<String, AttributeValue> attrValues = new HashMap<>();
        attrValues.put(":mt", AttributeValue.builder().s(targetMaterialType).build());
        attrValues.put(":cutoff", AttributeValue.builder().n(String.valueOf(cutoffTimestamp)).build());

        QueryRequest queryReq = QueryRequest.builder()
                .tableName(tableName)
                .indexName(gsiName) // 指定要查询的GSI
                .keyConditionExpression("materialType = :mt AND createTime <= :cutoff")
                .expressionAttributeValues(attrValues)
                .build();

        // 执行查询并处理结果
        try {
            QueryResponse response = ddbClient.query(queryReq);
            // 遍历结果,这里可以替换为业务处理逻辑
            response.items().forEach(item -> System.out.println("Found item: " + item));
        } catch (DynamoDbException e) {
            System.err.println("查询失败: " + e.getMessage());
            System.exit(1);
        } finally {
            ddbClient.close();
        }
    }
}

额外说明

  1. 跨materialType查询的情况:如果需要查所有类型下超过15分钟的数据,Query做不到(必须指定哈希键),这时候要么用Scan(性能差,不推荐大数据量),要么重新设计GSI——比如用固定值(比如"ALL_MATERIALS")作为哈希键,排序键还是createTime,这样就能一次Query覆盖所有数据。
  2. 分页处理:如果查询结果超过1MB(DynamoDB单页上限),需要通过response.lastEvaluatedKey()循环分页,直到返回的LastEvaluatedKey为空。
  3. 时间戳一致性:确保createTime和计算的cutoffTimestamp都是毫秒级时间戳,别混用秒级导致过滤错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:30:42