I am trying to pull the last item from a dynamodb table:
Every record is stored in this format:
{
"created_at": "01/30/17-11:54:00",
"entry_id": 1485798840,
"temp": 26
}
entry_id is a auto generated using the number of milliseconds from epoch.
created_at: is the hash key (partition key).
entry_id: is the range key.
temp: attribute.
I need to get last entry from the table, I am using this lines of codes:
ambient_temp_table01 table = new ambient_temp_table01();
table.setEntry_id(1485798840);
table.setCreated_at("01/30/17-11:54:00");
Condition condition_GT = new Condition();
condition_GT.setComparisonOperator("GT");
condition_GT.withAttributeValueList(new AttributeValue().withN("1485798840"));
DynamoDBQueryExpression<ambient_temp_table01> queryExpression = new DynamoDBQueryExpression<ambient_temp_table01>();
queryExpression.withHashKeyValues(table);
queryExpression.withRangeKeyCondition("entry_id",condition_GT);
queryExpression.setScanIndexForward(false);
queryExpression.setLimit(1);
List<ambient_temp_table01> list_records = mapper.query(ambient_temp_table01.class,queryExpression);
for (ambient_temp_table01 record : list_records) {
System.out.print("Last temp reading from ambient_temp_table01 is: ");
System.out.println(record.getTemp());
}
The problem is I don't know the values of the last hash key and range key entries in the table, but the query condition requires these values to be added using the method withHashKeyValues(table).
That's is the reason I use the ComparisonOperator to "GT", because my supposition is that it will get all items which entry_id is greater than that number 1485798840, but I don't get any result.
However if I change the comparator to "EQ" and leave the same numbers, just for testing, I get the value of the temp attribute, for the record that matches the hash and range key I previously set.