Indexing 如何仅使用boto 2.25.0通过全局二级索引查询DynamoDB2表?
这是我从常规DynamoDB表切换到具有全局二级索引的DynamoDB2表的任务的继续** 因此,我创建了如图所示的表,然后添加了以下两个元素:Indexing 如何仅使用boto 2.25.0通过全局二级索引查询DynamoDB2表?,indexing,global,amazon-dynamodb,boto,secondary-indexes,Indexing,Global,Amazon Dynamodb,Boto,Secondary Indexes,这是我从常规DynamoDB表切换到具有全局二级索引的DynamoDB2表的任务的继续** 因此,我创建了如图所示的表,然后添加了以下两个元素: table.put_item(data={'firstKey': 'key01', 'message': '{"firstKey":"key01", "comments": "mess 1 w/o secondKey"}'}) table.put_item(data={'firstKey': 'key02', 'secondKey':'skey01',
table.put_item(data={'firstKey': 'key01', 'message': '{"firstKey":"key01", "comments": "mess 1 w/o secondKey"}'})
table.put_item(data={'firstKey': 'key02', 'secondKey':'skey01', 'message': '{"firstKey":"key02", "parentId":"skey01", "comments": "mess 2 w/ secondKey"}'})
我现在要做的是通过(I)uniquefirstKey
值或(ii)uniquesecondKey
值检索项目。第一个很简单:
res1 = table.get_item(firstKey='key01')
res1['message']
我不知道怎么做第二个。这不起作用:
res2 = table.get_item(secondKey='skey01')
生成提供的键元素与架构不匹配。好的,这是意料之中的。当我这样做时:
res2 = table.query(secondKey='skey01',index='secondKeyIndex')
I get您必须指定多个要筛选的键
那么我如何让它工作呢?注意,当我有一个项目的secondKey
值时,我不知道它对应的firstKey
=====
更新:以下是我尝试过的其他几件事:
这个
产生
boto.dynamodb2.exceptions.QueryError: You must specify more than one key to filter on.
在下面的块中,query
语句没有产生任何错误
res2 = table.query(secondKey='skey01',secondKey__eq='skey01',index='secondKeyIndex')
for r in res2:
print res2['secondKey']
但是,的印刷品给了我
boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'secondKey' from 'secondKey' is not recognized.
查询参数的格式不同。
我没有试过,但我相信语法应该是:
res2 = table.query(secondKey__eq='skey01',index='secondKeyIndex')
可以使用LSI/GSI
请参阅此处的boto教程(搜索LSI,您将获得示例)。
DynamoDB2-boto v2.25.0:
添加完整的工作示例(使用dynamo local:进行了尝试)
执行的结果是:
91d4d056-1da3-42c6-801e-5b8e9c42a93f DUMMY-second
15c17b09-4975-419a-b603-427e4c765f03 DUMMY-second
dd947b7d-935e-458f-84d3-ed6cd4f32f5a DUMMY-second
还添加了确切的软件包(由于Dynamo1/2-有可能出错):
对于所有在使用query_2时寻找更新版本的人:请查看好吧,我在几天的时间里反复阅读了几次这一页,试图让它发挥作用。我知道这是可以完成的,因为我是通过AWS控制台完成的,但现在我需要弄清楚如何使用boto
以编程方式完成。你能给我展示一下上面例子的确切查询语法吗?谢谢,顺便说一句,如果我需要修改我创建表的方式来实现这一点,那也很好。我只需要能够通过firstKey
或secondKey
检索项目。好了,现在开始:我用一个完整的工作示例编辑了上面的答案。我刚刚尝试并确保了这项工作与迪纳摩DB本地。人们面临的一个常见问题是使用较旧版本的Dynamo Local。请确保下载最新版本并重试。我现在尝试了dynamodb_local_2013-12-12
conn = DynamoDBConnection(
host='localhost',
port=8000,
aws_access_key_id='DEVDB', #anything will do
aws_secret_access_key='DEVDB', #anything will do
is_secure=False)
tables = conn.list_tables()
print "Before Creation:", tables
table_name = 'myTable'
if table_name not in tables['TableNames']:
Table.create(table_name
, schema=[HashKey('firstKey')]
, throughput={'read': 5, 'write': 2}
, global_indexes=[
GlobalAllIndex('secondKeyIndex', parts=[HashKey('secondKey')], throughput={'read': 5, 'write': 3})]
, connection=conn
)
#print_table_details(conn, table_name)
table = Table(table_name, connection=conn)
item = Item(table, data={
'firstKey': str(uuid.uuid4()),
'secondKey': 'DUMMY-second'
})
item.save()
results = table.query(secondKey__eq='DUMMY-second', index='secondKeyIndex')
for res in results:
print res['firstKey'], res['secondKey']
91d4d056-1da3-42c6-801e-5b8e9c42a93f DUMMY-second
15c17b09-4975-419a-b603-427e4c765f03 DUMMY-second
dd947b7d-935e-458f-84d3-ed6cd4f32f5a DUMMY-second
from boto.dynamodb2.fields import HashKey, RangeKey, GlobalAllIndex
from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2.table import Table
from boto.dynamodb2.items import Item