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

查看:90
本文介绍了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)

执行此操作时,出现此错误查询条件缺少键架构元素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?

推荐答案

使用查询操作,您必须提供一个哈希键,没有定义为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调用中的多个哈希键中,您必须使用扫描(您可以使用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)

Ps,您的二级索引与范围键相同,是一个错误吗?

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

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

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