L-R
L-R

Reputation: 1232

Query dynamoDB with non hash key field (with boto / python)

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

Upvotes: 2

Views: 4308

Answers (1)

yadutaf
yadutaf

Reputation: 7132

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

Upvotes: 5

Related Questions