How to retrieve all the entries with hash key as a particular pattern in dynamo DB?
Asked Answered
M

1

5

I am new to amazon web services. I want to retrieve all the entries in the dynamo DB table which have a particular word in their hash key. It is similar to using like operator in oracle DB. How can I do it? If its not possible, how do I retrieve all the entries in the table without giving any constraint on the hash key so that later I can iterate over all of them to match the word?

Monkhmer answered 5/10, 2012 at 6:11 Comment(0)
S
16

General DynamoDB thoughts:

Please, do not think of DynamoDB as you would of MySQL. DynamoDB is only a key:value store with a couple of niceties.

The fundamental of key:value store is:

Either you know the key, and get your value;

Either you don't, and you get the whole table.

In DynamoDB specific case, you can

  1. Retrieve an item knowing the whole key with GetItem
  2. Retrieve all items under a given hash_key, possibly applying a filter, with Query
  3. Retrieve the whole table, possibly applying filters, with Scan

This said, Scan is both slow and expensive. You should never use it otherwise your design is most probably wrong.

Specific to your question:

In your case, it sounds like you will want to split your key into a

  • hash_key
  • range_key

Then you could use Query to get all the items sharing hash_key. With Query, you may apply filters like BEGINS_WITH to narrow you scope.

Please note that all (hash_key, range_key) tuples needs to be unique.

Sheepwalk answered 5/10, 2012 at 13:45 Comment(2)
It should be worth mentioning that even if you do a query, and want to apply a filter within the query - the results can only be a max of 1MB, meaning that if the query exceeds 1MB, you will have have all of your data to be filtered on.Aeromarine
Makes me wish AWS and blogs didn't market DynamoDB as a 'not only SQL' datastore. I've never worked with any key value store where you couldn't ask for the list of keys. In their documentation I've not seen a head-nod to the requirement for maintaining your keys in another table/lookup. @yadutaf's comment on Scans is spot on--it's almost like they added that feature so you can know when you're using the wrong datastore.Eve

© 2022 - 2024 — McMap. All rights reserved.