python - 如何使用 "range_key_condition"通过 boto 查询 DynamoDB 表?

标签 python amazon-web-services boto amazon-dynamodb

要获得 (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'

最佳答案

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

关于python - 如何使用 "range_key_condition"通过 boto 查询 DynamoDB 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9584730/

相关文章:

python - 如何匹配除特定数字以外的所有 3 位数字

python - 如何在 Blender Python 导出脚本中重新排序指向顶点的索引,以便正确连接我的模型?

python - 将多个 QSlider 连接到一个插槽

amazon-web-services - 如何添加或更改堆栈的标签?

amazon-web-services - AWS SAM 启动本地 api 返回 "Function name is required"错误

python - AWS 默认区域未在 boto 中使用 env 变量进行设置

python - 使用 boto 将文件从 S3 复制到 Google Cloud 存储

python - Django REST API 'get' 函数

java - 从外部源系统下载 AWS Lambda 的源代码

Python Boto Dynamodb 对范围键的小记录集检索性能非常慢