简体   繁体   English

正确的Boto aws DynamoDb .scan语法

[英]Correct Boto aws DynamoDb .scan syntax

I've been working with Dynamo & Python for a little while now however I've run into what appears to be mismatch in syntax. 我已经使用Dynamo和Python了一段时间,但是我遇到了语法上似乎不匹配的问题。

I read: 我读:

# All results.
>>> everything = users.scan()

# Look for last names beginning with "D".
>>> results = users.scan(last_name__beginswith='D')
>>> for res in results:
...     print res['first_name']
'Alice'
'John'
'Jane'

# Use an ``IN`` filter & limit.
>>> results = users.scan(
...     age__in=[25, 26, 27, 28, 29],
...     limit=1
... )
>>> for res in results:
...     print res['first_name']
'Alice'

From: http://boto.readthedocs.org/en/latest/ref/dynamodb2.html Which seems to agree with the scan function in boto table: https://github.com/boto/boto/blob/433f211b5eb93560916a4bd4a1dbf905e6c13a58/boto/dynamodb2/table.py 来自: http//boto.readthedocs.org/en/latest/ref/dynamodb2.html这似乎与boto表中的扫描功能一致: https : //github.com/boto/boto/blob/433f211b5eb93560916a4bd4a1dbf905e6c13a58/boto /dynamodb2/table.py

The issue lies in that when I try: 问题在于当我尝试:

def getByAdvertiser(adv):
    matchingTable=swfTable.scan(advertiser__eq=adv)
    return getTableElements(matchingTable)
def getTableElements(table):
    res=[]
    for t in table:
        res.append(t)
    return res

Which based on the above syntax should work given that swfTable is a valid table, .scan() returns elements, that "advertiser" is a dynamo table column, and there exists at least one element in "advertiser" that is equal to adv ("itunes.apple.com"). 假设swfTable是有效表,.scan()返回元素,“ advertiser”是发电机表列,并且在“ advertiser”中至少存在一个等于adv的元素,则基于上述语法的方法应该可以工作“ itunes.apple.com”)。 I get the following error however: 但是,我得到以下错误:

Traceback (most recent call last): File "/Users/tai/Documents/workspace/testSelenium/testS/ init .py", line 101, in forInFile() File "/Users/tai/Documents/workspace/testSelenium/testS/ init .py", line 95, in forInFile dynamoAccess.getByAdvertiser("itunes.apple.com") File "/Users/tai/Documents/workspace/testSelenium/testS/dynamoAccess.py", line 34, in getByAdvertiser matchingTable=swfTable.scan(advertiser__eq=adv) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/boto/dynamodb/table.py", line 518, in scan return self.layer2.scan(self, *args, **kw) TypeError: scan() got an unexpected keyword argument 'advertiser__eq' 追溯(最近一次通话):文件“ / Users / tai / Documents / workspace / testSelenium / testS / init .py”,第101行,位于forInFile()文件“ / Users / tai / Documents / workspace / testSelenium / testS / init .py“,第95行,位于forInFile dynamoAccess.getByAdvertiser(” itunes.apple.com“)文件” /Users/tai/Documents/workspace/testSelenium/testS/dynamoAccess.py“,第34行,位于getByAdvertiser matchingTable = swfTable中.scan(advertiser__eq = adv)文件“ /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/boto/dynamodb/table.py”,行518,在扫描中返回self.layer2.scan (自身,* args,** kw)TypeError:scan()得到了意外的关键字参数“ advertiser__eq”

I don't see how I'm not following the documentation's syntax. 我看不到我没有遵循文档的语法。

However when I look at other Boto dynamodb questions they use syntax such as: 但是,当我查看其他Boto dynamodb问题时,它们使用的语法如下:

results = self.table.scan(scan_filter={'asset': dynamodb.condition.EQ(asset)})

from: 从:

Trouble getting a dynamodb scan to work with boto 无法使动力发电机扫描与Boto配合使用

Or: 要么:

all_query = table.scan(attributes_to_get=['something'])

From: boto python dynamodb scan attributes_to_get 来自: Boto Python dynamodb扫描attribute_to_get

Which looks nothing like what I'm using or anything that I've seen documented. 看起来与我正在使用的东西或我所看到的记录不一样。

EDIT: 编辑:

I believe the issue may be that I have been using dynamodb1 instead of 2. 我认为问题可能在于我一直在使用dynamodb1而不是2。

aws_dynamo_table="decompiled_swf_text"
conn= S3Connection(aws_access_key_id,aws_secret_access_key);
dynamoConn = boto.connect_dynamodb(aws_access_key_id, aws_secret_access_key)
dTable = dynamoConn.get_table(aws_dynamo_table)

Would this be solved by using dynamodb2? 使用dynamodb2是否可以解决? If so how can I set that up? 如果可以,我该如何设置? I'm trying: 我正在努力:

dynamoConn = dynamodb2.layer1.DynamoDBConnection(region=RegionInfo(name=aws_dynamo_region,endpoint='dynamodb.us-east-1.amazonaws.com'),aws_access_key_id,aws_secret_access_key)

However I don't see how I can then query and scan tables... I don't see an available function. 但是,我看不到如何查询和扫描表...我看不到可用的功能。

The two examples that you found are both using traditional/low-level API . 您找到的两个示例都使用传统/低级API for example in the old API, the scan function will take a scanFilter as a parameter. 例如,在旧的API中, 扫描功能将使用scanFilter作为参数。

And syntax like 和类似的语法

>>> results = users.scan(
...     age__in=[25, 26, 27, 28, 29],
...     limit=1
... )

is defined in new/high level API . 新的/高级API中定义。

You should definitely switch to DynamoDB2. 您绝对应该切换到DynamoDB2。 from your error output 从您的错误输出

in scan return self.layer2.scan(self, *args, **kw) TypeError:

it seems you were using DynamoDB instead of DynamoDB2 because layer2 API is defined in DynamoDB api and it goes away in DynamoDB2. 似乎您使用的是DynamoDB而不是DynamoDB2,因为layer2 API在DynamoDB api中定义,而在DynamoDB2中消失了。

So switch boto DynamoDB2 因此,切换到Boto DynamoDB2

1) get your table 1) 得到你的桌子

2) use table.scan(age__in=[25, 26, 27, 28, 29], limit=1) see here 2)使用table.scan(age__in = [25,26,27,28,29],limit = 1)参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM