5

要获得 (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"}
      )

使用 boto v2.2.2-dev,我总是得到空结果

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

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}

上面没有“range_key_condition”也没关系

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'
4

2 回答 2

5

如果您使用的是最新版本的 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))

它应该可以工作,尽管您必须更新您的 boto 代码,因为我刚刚在调查此问题时发现了 BETWEEN 中的错误(请参阅https://github.com/boto/boto/issues/620)。

于 2012-03-06T19:38:13.123 回答
1

API来看,您是否可以包含多个条件尚不清楚。如果您只执行其中一个条件,它是否有效?此外,还有一个 BETWEEN 运算符可能对您有用:range_key_condition = {(0,10000):"BETWEEN"},但如果您的值是真正的字符串而不是数字,它可能不起作用。我没有使用过这个 API,所以我可能会在 boto 邮件列表或IRC上询问。

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

于 2012-03-06T14:51:30.847 回答