How to query List of Maps in DynamoDb

Viewed 8702

I have of the non-primary key column in DynamoDb which is of List type as this:

logs=[{
m1:{"key1":"pair1",
"key2":"pair2",
"timestamp":2189321.212
},
m2:{"key1":"pair1",
"key2":"pair2",
"timestamp":2112321.212
},
...
}]

So problem statement is I want to get a list of items, which are 30 days old. I am not able to query this one.

3 Answers

It is not possible to do that directly using DynamoDB functions.

You could load and serialise each table log item into an object in your code, then sort and filter the objects. If you had a lot of log files this could use up a lot of memory.

If you are in control of how the log data is stored, you may want to remodel your tables, so that the logs are stored as table items. You could make log id your partition key and the timestamp a range key, which would make time based queries very fast.

If you went down that route, be aware that you would need to make your timestamp a number in DynamoDB in order to use it as a range key.

Let me know if below is useful.

I had my data inserted like this through CLI:

enter image description here

This is how it would look once entered: enter image description here

And through Java code simply mapped it like this. This works. enter image description here

From DynamoDB you cannot query based on the values of document types like you desire. Not unless you want to scan the whole table and do it in memory which I'm guessing you don't.

An alternate solution would be for you to index your data in AWS Elasticsearch service as well as DynamoDB. You can feed ES the full JSON and it will index every property so that you can run the kind of queries your are talking about. Obviously this is a project of its own but you have that option if you want to keep the JSON structure you currently have in DynamoDB.

Related