繁体   English   中英

无法按日期查询或扫描 DynamoDb

[英]Failing to query or scan DynamoDb by date

我有一个由一些记录组成的表和一个created_on列,它是一个日期(存储为自纪元以来的毫秒数,类型为N )。

我希望能够查询在今天日期之前创建的所有记录,但我遇到了很多问题。

我尝试在created_on上添加全局二级索引,并尝试使用表达式created_on <= :v1进行查询。 然而,这给出了错误com.amazonaws.AmazonServiceException: Query key condition not supported

一些谷歌搜索让我相信我需要使用 scan 而不是 query 和过滤器表达式。 所以我现在正在尝试:

Map<String, AttributeValue> args = //build map with key ':v1' and value of date's timestamp here


DynamoDBScanExpression q = new DynamoDBScanExpression()
        .withIndexName('created_on-index')
        .withFilterExpression("created_on  > :v1")
        .withExpressionAttributeValues(args);


return mapper.scan(type, q);

这运行没有错误,但返回 0 条记录。 我已经验证它返回的毫秒数是今天的日期,并且表中有 3 条记录符合条件。

有任何想法吗?

看来 1) 列需要映射为字符串,2) 日期需要格式化为 UTC 字符串。 以下代码正在运行:

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));;

AttributeValue mapped = new AttributeValue();
mapped = mapped.withS( dateFormatter.format( (Date) dateVal ) );

Map<String, AttributeValue> args = Collections.singletonMap(":v1", mapped);

DynamoDBScanExpression q = new DynamoDBScanExpression()
        .withIndexName('created_on-index')
        .withFilterExpression("created_on  <= :v1")
        .withExpressionAttributeValues(args);

取自: http : //docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.QueryScanExample.html

使用查询仍然不起作用,并且可能需要进行全表扫描。 超级反直觉,但至少它现在有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM