2

I'm using dynamoDB with boto, and having a bit of a problem in the design/query of my table.

I'd like my data to look something like

+---------------------------------------+
hash_key    account_id    mykey
-----------------------------------------
1           12345         myvalue1
2           12345         myvalue2
3           12345         myvalue3
4           123456        myvalue4
+---------------------------------------+

And then retrieve all data for account 12345. Looking at the boto docs, I always need to have the hash_key available. I know how I would query this standard SQL / MongoDB, but I can't find a solution for boto. I assume this is possible? Thanks!

EDIT: This seems to work

+---------------------------------------+
hash_key    range_key    mykey
-----------------------------------------
12345       12568        myvalue1
12345       53890        myvalue2
12345       12322        myvalue3
123456      23432        myvalue4
+---------------------------------------+

Followed by

> res = table.query(hash_key='12345')
> for item in res:
>    print i

Since I want to grab all the entries with account # 12345, regardless of the range_key, I need to query instead of get_item

1
  • Did you ever experience a 'UnknownFilterTypeError' when querying like this? Commented Oct 20, 2013 at 13:46

1 Answer 1

5

I would use the account_id as the hash_key along with some range_key to differentiate them.

In DynamoDB, the primary key is composed of a (hash_key, range_key) range_key being optional. This tuple needs to be unique. Note that you will need the whole tuple to access a given element with get_item.

Having an 'auto_increment' hash_key is a bad habit from the SQL world.

If you want to know more on this subject, I wrote some background do on modeling data in the dynamodb-mapper documentation: http://dynamodb-mapper.readthedocs.org/en/latest/api/model.html#auto-increment-when-to-use

Sign up to request clarification or add additional context in comments.

6 Comments

that's what I wanted originally, but the hash_key needs to be unique!
Yeah, use range_key to differentiate them. If you have more background on the model, I can try to suggest one.
I see - just tested, and indeed dynamoDB allows the same hash_key if you add the range_key. I guess the value I give to range_key is irrelevant (as long as it's unique), since I only use it to differentiate the entries? The data are actually GPS points that are inserted periodically. Thanks!
You're welcome. I updated my answer to reflect your comment. Note that you will need the whole (hash_key, range_key) tuple to access a random element. Btw, If this solves your issue, you may consider accepting this answer :)
Also added an edit - looks like query is what I was after - not sure of the performance implications here though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.