如何使用“range_key_condition”查询带有boto的DynamoDB表?

时间:2021-08-21 16:53:13

To get range_key between (0,9999), can I do it this way?

要获得介于(0,9999)之间的range_key,我可以这样做吗?

conn = boto.connect_dynamodb()
table = conn.get_table("mytable")
...
result = table.query(
      hash_key = "66", 
      range_key_condition = {"0":"GE", "9999":"LE"}
      )

with boto v2.2.2-dev, I always get empty results

EDIT: This is a another error sample:

编辑:这是另一个错误示例:

In [218]: qa = taa.query(hash_key = "1")

In [219]: qa.next()
Out[219]: {u'attra': u'this is attra', u'key': u'1', u'range': 1.1}

It's OK without "range_key_condition" above

In [220]: qa = taa.query(hash_key = "1", range_key_condition = {0.1: "GE"})

In [221]: qa.next()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/user/python/enva/<ipython-input-221-dba0a498b6e1> in <module>()
----> 1 qa.next()

/home/user/python/enva/local/lib/python2.7/site-packages/boto-2.2.2_dev-py2.7.egg/boto/dynamodb/layer2.pyc
in query(self, table, hash_key, range_key_condition,
attributes_to_get, request_limit, max_results, consistent_read,
scan_index_forward, exclusive_start_key, item_class)
    559         """
    560         if range_key_condition:
--> 561             rkc = self.dynamize_range_key_condition(range_key_condition)
    562         else:
    563             rkc = None

/home/user/python/enva/local/lib/python2.7/site-packages/boto-2.2.2_dev-py2.7.egg/boto/dynamodb/layer2.pyc
in dynamize_range_key_condition(self, range_key_condition)
    83         structure required by Layer1.
    84         """
---> 85         return range_key_condition.to_dict()
   86
   87     def dynamize_scan_filter(self, scan_filter):

AttributeError: 'dict' object has no attribute 'to_dict'

2 个解决方案

#1


5  

If you are using the latest version of boto (and it looks like you are) the way conditions have been changed from previous versions in an attempt to make the query more readable. Try this:

如果您正在使用最新版本的boto(看起来像你),那么条件已经从以前的版本更改为试图使查询更具可读性。尝试这个:

from boto.dynamodb.condition import *
conn = boto.connect_dynamodb()
table = conn.get_table("mytable")
...
result = table.query(
  hash_key = "66", 
  range_key_condition = BETWEEN(0, 9999))

It should work although you will have to update your boto code since I just discovered a bug in BETWEEN while investigating this question (see https://github.com/boto/boto/issues/620).

它应该可以工作,虽然你必须更新你的boto代码,因为我在调查这个问题时发现了BETWEEN中的一个错误(参见https://github.com/boto/boto/issues/620)。

#2


1  

From the API, It's not clear that you're allowed to include more than one condition. Does it work if you just do one of the conditions alone? Also, there is a BETWEEN operator which may work for you: range_key_condition = {(0,10000):"BETWEEN"}, but it may not work if your values are truly strings rather than numbers. I haven't used this API, so I'd probably ask on the boto mailing list or IRC.

从API中,您不清楚是否允许包含多个条件。如果你只是单独使用其中一个条件,它会起作用吗?此外,还有一个BETWEEN运算符可能适合您:range_key_condition = {(0,10000):“BETWEEN”},但如果您的值是真正的字符串而不是数字,它可能不起作用。我没有使用过这个API,所以我可能会在boto邮件列表或IRC上询问。

Update: Based on the error message that you just added and looking at the Boto code on github, I think this is a bug in Boto. Table.query just calls Layer2.query. Layer2.query expects range_key_condition to be a Condition object (which has a to_dict method), but you provide a regular dict (which obviously doesn't have a to_dict method). Table.query should be converting your dict to a Condition object and providing it to Layer2.query, but it doesn't. I'd go to the mailing list or IRC for help.

更新:根据您刚刚添加的错误消息并查看github上的Boto代码,我认为这是Boto中的一个错误。 Table.query只调用Layer2.query。 Layer2.query期望range_key_condition是一个Condition对象(具有to_dict方法),但是你提供了一个常规的dict(显然没有to_dict方法)。 Table.query应该将您的dict转换为Condition对象并将其提供给Layer2.query,但事实并非如此。我会去邮件列表或IRC寻求帮助。

#1


5  

If you are using the latest version of boto (and it looks like you are) the way conditions have been changed from previous versions in an attempt to make the query more readable. Try this:

如果您正在使用最新版本的boto(看起来像你),那么条件已经从以前的版本更改为试图使查询更具可读性。尝试这个:

from boto.dynamodb.condition import *
conn = boto.connect_dynamodb()
table = conn.get_table("mytable")
...
result = table.query(
  hash_key = "66", 
  range_key_condition = BETWEEN(0, 9999))

It should work although you will have to update your boto code since I just discovered a bug in BETWEEN while investigating this question (see https://github.com/boto/boto/issues/620).

它应该可以工作,虽然你必须更新你的boto代码,因为我在调查这个问题时发现了BETWEEN中的一个错误(参见https://github.com/boto/boto/issues/620)。

#2


1  

From the API, It's not clear that you're allowed to include more than one condition. Does it work if you just do one of the conditions alone? Also, there is a BETWEEN operator which may work for you: range_key_condition = {(0,10000):"BETWEEN"}, but it may not work if your values are truly strings rather than numbers. I haven't used this API, so I'd probably ask on the boto mailing list or IRC.

从API中,您不清楚是否允许包含多个条件。如果你只是单独使用其中一个条件,它会起作用吗?此外,还有一个BETWEEN运算符可能适合您:range_key_condition = {(0,10000):“BETWEEN”},但如果您的值是真正的字符串而不是数字,它可能不起作用。我没有使用过这个API,所以我可能会在boto邮件列表或IRC上询问。

Update: Based on the error message that you just added and looking at the Boto code on github, I think this is a bug in Boto. Table.query just calls Layer2.query. Layer2.query expects range_key_condition to be a Condition object (which has a to_dict method), but you provide a regular dict (which obviously doesn't have a to_dict method). Table.query should be converting your dict to a Condition object and providing it to Layer2.query, but it doesn't. I'd go to the mailing list or IRC for help.

更新:根据您刚刚添加的错误消息并查看github上的Boto代码,我认为这是Boto中的一个错误。 Table.query只调用Layer2.query。 Layer2.query期望range_key_condition是一个Condition对象(具有to_dict方法),但是你提供了一个常规的dict(显然没有to_dict方法)。 Table.query应该将您的dict转换为Condition对象并将其提供给Layer2.query,但事实并非如此。我会去邮件列表或IRC寻求帮助。