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

倖福魔咒の 提交于 2019-12-11 04:19:10

问题


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:


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



来源:https://stackoverflow.com/questions/12484635/query-dynamodb-with-non-hash-key-field-with-boto-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!