Reputation: 364
I have a use-case where I have records (device mac addresses) in a dynamodb table. I want to fetch records and process them. Is there a way that I can get records in sorted order and limit the result by 10? DynamoDB mandates to provide a hash-key in the query expression, which I don't have in my case. Even in GSI, there has to be a hash-key in the query (even if it is different than hash key of main table). Any help will be highly appreciated.
Upvotes: 1
Views: 290
Reputation: 33392
The only way to get both ordering and limits is to use queries, not scans.
But you seem not to have a hash key, so you cannot use queries. What are you options?
Two comes to my mind.
First one: store createAt
not as a timestamp, but as a date and a timestamp.
Create a global secondary index with a hash key set to date and range set to timestamp.
This way you'll be able to query this index with sorting and limits.
However, if you have less then your desired limits items in a single day it wont work (you'll need to query previous date).
Second: use DynamoDB Streams + Lambda to get notified about new entries and store them in another place deleting the oldest one, thus forming an LRU cache of the desired size (limit). You can even use scans to search the oldest item in this cache because its size won't be big.
Upvotes: 1