boto dynamodb2:我可以仅使用范围键查询表吗? [英] boto dynamodb2: Can I query a table using range key only?

查看:19
本文介绍了boto dynamodb2:我可以仅使用范围键查询表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个 python 应用程序中,我正在使用 boto,我只想使用范围键查询 dynamodb 表.我不想使用扫描.

In one of my python application, I am using boto and I want to query a dynamodb table using range key only. I don't want to use scan.

评级表架构

ratings = Table.create('ratings', schema=[
    HashKey('user_id', data_type=NUMBER),
    RangeKey('photo_id', data_type=NUMBER)
], throughput={
    'read': 5,
    'write': 15,
}, indexes = [
    AllIndex('rating_allindex', parts=[
        HashKey('user_id', data_type=NUMBER),
        RangeKey('photo_id', data_type=NUMBER)
    ])
])


from boto.dynamodb2.table import Table
ratings = Table('ratings')
# photo_id is range_key and user_id is hash_key
ratings_list = ratings.query(photo_id__eq=1)

这样做时,我收到此错误 Query condition missing key schema element user_id.同样,我想我可以给我的 hash_key 一个过滤条件

On doing so, I get this error Query condition missed key schema element user_id. Again, I thought I could give a filter condition to my hash_key

ratings_list = ratings.query(user_id__gte=1, photo_id__eq=1)

但它显示错误,不支持查询键条件.我想 hash_key 只允许使用过滤器 __eq.如何实现我想要的?

But it showed the error, Query key condition not supported. I suppose only the filter __eq is allowed with hash_key. How do I achieve what I want?

推荐答案

当使用 Query 对 DynamoDB 的操作,您必须提供 一个 哈希键,它没有定义为 range_key_conditions 的一部分,如您在文档中看到的 -所以你必须使用你已经知道的 user_id_eq.

When using a Query operation on DynamoDB you must supply one hash key, it is not defined as part of the range_key_conditions as you can see in the documentation - so you will have to use user_id_eq as you already figured out.

如果您需要在一次 API 调用中从多个哈希键中获取行,则必须使用 Scan(您可以使用 batchGet 获取多行,但这与您的场景无关)

If you need to get rows from more than one hash key in one API call, you must use Scan (you can fetch multiple rows using batchGet but this is irrelevant to your scenario)

P.s,你的二级索引好像和 Range 键一样,是不是搞错了?

P.s, it appears your secondary index is the same as the Range key, is that a mistake?

这篇关于boto dynamodb2:我可以仅使用范围键查询表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆