I have a table consisting of some records, and a created_on
column which is a date (stored as milliseconds since epoch, with an N
type).
I'd like to be able to query all records which were created before today's date, but I'm having a lot of issues with it.
I tried adding a global secondary index on created_on
, and I tried a query with the expression created_on <= :v1
. However this gave the error com.amazonaws.AmazonServiceException: Query key condition not supported
Some googling led me to believe that I'd need to use scan rather than query, with a filter expression. So I'm now attempting:
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);
This runs without an error, but returns 0 records. I've verified that the milliseconds it returns are of today's date, and there are 3 records in the table for which the condition should match.
Any ideas?
It appears that 1) the column needs to be mapped as a string, 2) The date needs to be formatted as a UTC string. The following code is working:
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);
Taken from: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.QueryScanExample.html
Using a query still doesn't work, and likely this will require a full table scan. Super counter intuitive, but at least it now works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.